Search code examples
gitgithub

How to remove a branch locally?


I have a master and a dev branch in my repository. I want to remove the master branch from my computer so that I don't accidentally commit to it (it's happened..).

There are questions on here about how to delete branches locally and remotely, but I haven't been able to find out how to only delete a branch locally.

One answer said to use this:

git branch -d local_branch_name

But I tried that and the branch still shows up in the GitHub application.


Solution

  • I think (based on your comments) that I understand what you want to do: you want your local copy of the repository to have neither the ordinary local branch master, nor the remote-tracking branch origin/master, even though the repository you cloned—the github one—has a local branch master that you do not want deleted from the github version.

    You can do this by deleting the remote-tracking branch locally, but it will simply come back every time you ask your git to synchronize your local repository with the remote repository, because your git asks their git "what branches do you have" and it says "I have master" so your git (re)creates origin/master for you, so that your repository has what theirs has.

    To delete your remote-tracking branch locally using the command line interface:

    git branch -d -r origin/master
    

    but again, it will just come back on re-synchronizations. It is possible to defeat this as well (using remote.origin.fetch manipulation), but you're probably better off just being disciplined enough to not create or modify master locally.