user2075599
user2075599

Reputation:

How to delete a local branch

I have the simplest possible scenario you could possibly come up with :) This does not require any of the "stuff" about local/remote, are there commits etc (there are no pushes or commits involved at all as you will see below). Note I am relatively new to git / I am working within the following environment: C# coding, Visual Studio 2019, everything integrated with Azure DevOps utilizing private git repositories.

Also, I have the latest version of Git for Windows installed on my Windows 10 computer, and the Git Extension for Visual Studio extension installed inside Visual Studio 2019 Professional.

Otay. So here is what I have done (i.e., what I mean by the "simplest possible scenario"):

  1. In my browser, I look in the Azure DevOps project, Branches. There is a master branch. OK. I don't do anything in Azure DevOps.

  2. I switch to my Visual Studio 2019 Professional. [Note that I have everything set up properly, I have connected to the Azure DevOps repository in the visual studio Team Explorer tab, have all the requisite permissions/configurations etc.] I see in the Team Explorer tab, the master branch of my project. I right-click on the master branch and say I want to create a new local branch. I name it "WDCTest". I click on the Create button. The WDCTest branch gets created. Down at the bottom of the Team Explorer window I now see that it I am working with the "WDCTest" branch instead of the "master" branch.

  3. I look back in the Azure DevOps web page, I see that it knows nothing about the WDCTest branch. (Which in this case is what I want; at this point in time I just want to look at the code inside visual studio to familiarize myself with the codebase and don't plan on making any actual mods, commits etc from this branch.)

  4. I open up the Visual Studio solution that corresponds to the WDCTest branch. I look at a few things. I do NOTHING. I change NOTHING. I now decide, I don't want this local branch called WDCTest after all. I want to make it be as if I had never even created it from the master branch. I close the Visual Studio solution. I right-click on the WDCTest branch in the team explorer and there is a Delete option, but it is grayed out.

How do I get rid of the WDCTest branch???

Before you bang out, just do "$ git branch -D ", that doesn't work.

I go to my windows start button (if the following is even what I am supposed to do, remember I am a beginner with git stuff), scroll down to the Git entry, choose the "Git Bash" option, the little black window opens up and at the "$" prompt, I type "git branch -D WDCTest".

I get the following error message:

fatal: not a git repository (or any of the parent directories): .git

Help please.

I can't post a screenshot because I am working on a highly secured other system, but here is ALL of the text that is seen inside the "black" command window that has the window caption "MINGW64:/c/Users/warren.connors" after choosing the Git Bash windows start entry:

warren.connors@E5470-01380 MINGW64 ~
$ git branch -D WDCTest
fatal: not a git repository (or any of the parent directories): .git

warren.connors@E5470-01380 MINGW64 ~
$

So to summarize, we have a only-created-locally branch, which has had no changes made of any kind, and about which nothing whatsoever has been pushed back to the Azure DevOps server such that it doesn't even show up in the list of branches on the DevOps branches web page. Surely there must be a way to just delete it. Most of the other related s/o questions have to do with the scenario in which the existence of the local branch WAS pushed back to the Azure DevOps server. I even went thru a tutorial that did all of that and then I was able to delete the local branch at the end. Surely it isn't necessary to push this only-created-locally branch back to the server first in order to delete it off my local machine? Or is that what is necessary?

Upvotes: 4

Views: 27106

Answers (3)

Paul Okoli
Paul Okoli

Reputation: 101

On the terminal of your code editor, run: git branch -d branch-name Replace "branch-name" with the name of the local branch you wish to delete. That solved the problem for me, you should try it.

Upvotes: 2

Chuck Lu
Chuck Lu

Reputation: 191

You should locate the local git repository folder (you cloned it to local computer) in file explorer first.
Then right click on blank space of the repository folder, choose git bash here.(Make sure you have installed git for windows correctly)
Then execute the command git branch -D WDCTest

Upvotes: 1

user2075599
user2075599

Reputation:

Probably could have just switched back to the master branch in the Team Explorer at the bottom of the visual studio Team Explorer tab, then you can right-click delete the local-only branch in the "Active Git Repositories" list in the Team Explorer. (I would imagine that the bash command would also have worked after switching back to the master branch in the Team Explorer at the bottom of the visual studio Team Explorer tab.)

Upvotes: 9

Related Questions