250

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.

Nate
  • 26,164
  • 34
  • 130
  • 214
  • What's the output from that command? What branch are you on? How is it appearing in the console? – SLaks Feb 01 '15 at 02:04
  • @SLaks I had a type in my question. In the console it actually does say the branch was deleted, but in the GitHub application (even after restarting it) the branch and commit are still visible. The output is `Deleted branch master (was e8a8e29).` – Nate Feb 01 '15 at 02:05
  • GitHub for Windows shows remote branches too – SLaks Feb 01 '15 at 02:06
  • @SLaks So after deleting the branch locally, if I click on it in the GitHub for Windows application is it just re-cloning it? I'm wanting to prevent myself from being able to make changes to the master branch directly. – Nate Feb 01 '15 at 02:06
  • I understand what you are trying to do -protect your remote Master. It would be better if you (or you GIT admin) created rules preventing those unauthorized from pushing to Master. – davidhartman00 Oct 12 '20 at 19:45

9 Answers9

201

To Force Delete a Local Branch:

$ git branch -D <branch-name>

[NOTE]:

-D is a shortcut for --delete --force.


Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
135

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.

torek
  • 448,244
  • 59
  • 642
  • 775
32

As far I can understand the original problem, you added commits to local master by mistake and did not push that changes yet. Now you want to cancel your changes and hope to delete your local changes and to create a new master branch from the remote one.

You can just reset your changes and reload master from remote server:

git reset --hard origin/master
Eugene Kaurov
  • 2,356
  • 28
  • 39
16

The Github application for Windows shows all remote branches of a repository. If you have deleted the branch locally with $ git branch -d [branch_name], the remote branch still exists in your Github repository and will appear regardless in the Windows Github application.

If you want to delete the branch completely (remotely as well), use the above command in combination with $ git push origin :[name_of_your_new_branch]. Warning: this command erases all existing branches and may cause loss of code. Be careful, I do not think this is what you are trying to do.

However every time you delete the local branch changes, the remote branch will still appear in the application. If you do not want to keep making changes, just ignore it and do not click, otherwise you may clone the repository. If you had any more questions, please let me know.

12

After deleting branch using:

git branch -d BranchName

To remove branches that no longer exist in the remote repository use:

git fetch -p

-p indicates prune, to remove branches from local repository that doesn't exist in the remote.

Hemanth Kollipara
  • 902
  • 11
  • 16
3

You can delete multiple branches on windows using Git GUI:

  1. Go to your Project folder
  2. Open Git Gui: enter image description here
  3. Click on 'Branch': enter image description here
  4. Now choose 'Delete': enter image description here
  5. If you want to delete all branches besides the fact they are merged or not, then check 'Always (Do not perform merge checks)' enter image description here
Mehdi Bouzidi
  • 1,937
  • 3
  • 15
  • 31
2

you need switch into another branch and try the same.

git branch -d

  • 2
    Is this supposed to be an answer? Or a clarification qustion? Or a plain question? It is so short I cannot tell. Would you like to elaborate? – Yunnosch Sep 20 '19 at 17:47
2
git branch | grep -v "develop" | grep -v "master" | xargs git branch -D

Simple and easy. It will delete all the except you are working on and "develop" and "master".

alper
  • 2,919
  • 9
  • 53
  • 102
Ravi Raj
  • 27
  • 3
1

By your tags, I'm assuming your using Github. Why not create some branch protection rules for your master branch? That way even if you do try to push to master, it will reject it.

1) Go to the 'Settings' tab of your repo on Github.

2) Click on 'Branches' on the left side-menu.

3) Click 'Add rule'

4) Enter 'master' for a branch pattern.

5) Check off 'Require pull request reviews before merging'

I would also recommend doing the same for your dev branch.