39 Answers
39

Executive Summary

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d branch_name
$ git branch -D branch_name

Note: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status." [Source: man git-branch]

Delete Remote Branch [Original Answer from 5-Jan-2010]

Deleting Remote Branches

Suppose you’re done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote’s master branch (or whatever branch your stable code-line is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your server-fix branch from the server, you run the following:

Boom. No more branch on your server. You may want to dog-ear this page, because you’ll need that command, and you’ll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch] syntax that we went over a bit earlier. If you leave off the [localbranch] portion, then you’re basically saying, “Take nothing on my side and make it be [remotebranch].”

I issued git push origin :bugfix and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).

If you know the syntax git push origin local_branch:remote_branch, then the syntax to delete a branch with git push origin :remote_branch is kind of cute. There's a void before the :
– Marc-André LafortuneMay 11 '12 at 4:05

250

Don't forget to do a git fetch --all --prune on other machines after deleting the remote branch on the server. ||| After deleting the local branch with git branch -d and deleting the remote branch with git push origin --delete other machines may still have "obsolete tracking branches" (to see them do git branch -a). To get rid of these do git fetch --all --prune.
– Trevor Boyd SmithMay 27 '15 at 16:51

If your local branch is not merge with master and ran 'git branch -d your_branch then you will error like error: The branch 'your_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D your_branch'.
– geeksOct 31 '15 at 12:59

9

I would suggest using -d instead of -D because it is safer. If -d fails due to unmerged commits then you need to assess that and if it is definitely OK to remove then use -D.
– roobyFeb 2 '16 at 3:47

4

Others with repository clones where remote branches have been removed should run git remote prune <name> (e.g. git remote prune origin) in order to locally remove stale branches that no longer exist in the remote.
– code_dreddApr 18 '16 at 23:07

Additional Details

Note that deleting the remote branch X from the command line using a git pushwill also delete the local remote-tracking branchorigin/X, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune or git fetch -p, though it wouldn't hurt if you did it anyway.

You can verify that the remote-tracking branch origin/X was also deleted by running the following:

Pruning the obsolete local remote-tracking branch origin/X

If you didn't delete your remote branch X from the command line (like above), then your local repo will still contain (a now obsolete) remote-tracking branch origin/X. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.

A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch with the --prune or shorter -p. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:

"git fetch" learned--all and --multipleoptions, to run fetch from
many repositories, and --prune option to remove remote tracking
branches that went stale. These make "git remote update" and "git
remote prune" less necessary (there is no plan to remove "remote
update" nor "remote prune", though).

Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p, you can avoid making the extra network operation by just manually removing the branch(es) with the --remote or -r flags:

From your illustration, I can see there are local clone repo and remote origin repo. So there are at least two physical branches. Where is the third branch to delete? Is the third branch only a pointer pointing to a commit in the local clone repo?
– huggieFeb 18 '16 at 2:00

5

@huggie that's pretty much correct. Branches in Git are just bookmarks attached to commits. So in my graphs above, there are X and origin/X bookmarks in the local clone (2 branches), and then there is X on the remote (making 3 branches).
– user456814Feb 23 '16 at 7:33

6

+1 for the remote tracking branch. This branch is what causes issues when you clone someone else's branch. It keeps on tracking your commits and asking you if you want to push to that person's branch.
– Kermit_ice_teaJun 21 '17 at 19:48

Simply do git push origin --delete to delete your remote branch ONLY, add the name of the branch at the end and this will delete and push it to remote at the same time...

Also, git branch -D, which simply delete the local branch ONLY!...

-D stands for --delete --force which will delete the branch even it's not merged(force delete), but you can also use -d which stands for --delete which throw an error respective of the branch merge status...

git branch -a will display local and remote branches.It will be help for you diagram introduce.
– LoranceChenJul 27 '17 at 3:01

note that if you are setting on the branch you want to delete, you need to checkout a branch other than the one you need to delete (eg: master) before deleting the local branch.
– BaDr AmerMay 28 '18 at 8:43

only the references are deleted. Even though the branch is actually removed on the remote the references to it still exists in the local repositories of your team members. This means that for other team members the deleted branches are still visible when they do a git branch -a.

You should clarify that the above git push operation deletes the local branch and the remote branch.
– Annika BackstromMay 21 '13 at 13:51

17

Note that git remote prune is a somewhat obsolete way to remove obsolete remote-tracking branches, the newer way to do it is to use git fetch --prune or git fetch -p.
– user456814Jun 11 '14 at 16:30

1

@RRMadhav, indeed you won't see the deleted branch after deleting it since the reference to the remote branch will be removed for you locally. Anyone else on your team that has checked out that branch will still have that reference and will still see it unless they prune the branch.
– pfrenssenDec 5 '14 at 14:27

If your origin is a Atlassian Stash and the branch is set as the default, you will get an error "By default, deleting the current branch is denied...". I had to change the default branch in Stash to point to another branch before I could delete.
– neoscribeDec 12 '14 at 0:29

This is perfectly simple as you've done it, but fyi git also lets you make custom commands. Put git push origin --delete $1 in a file on your path called git-shoot and git shoot branchname will work too.
– mahemoffOct 14 '15 at 7:09

I only started using Github this year, so I was wondering why this was such a highly rated question, and why none of the top answers were suggesting to just delete it from the Github web interface! Interesting that it's only a recent addition.
– Cam JacksonSep 11 '13 at 12:18

This is what I was looking for. My own shell function alias didn't work (Unexpected EOF) and I couldn't figure out why, but this works great! The only change I made was replacing && with ; so that even if the first command fails the second will still execute (sometimes only local or only remote exists).
– user1021726Dec 16 '14 at 8:55

I didn't downvote, but my thinking is that it isn't substantively helping. The question is obviously asking for a more commandline type answer without having to use an external program, if people were clicking into here, they likely won't be looking for a github for desktop ways.
– DaemedeorNov 6 '15 at 11:51

9

@Daemedeor , I dissagree. In 2010 when the OP asked the question, the UI way of doing it didn't exist and the only option was command line. To indicate that you want a command line only option it should be stated in the question or with the tag, command-line-interface, which in this case is no present.
– EricNov 6 '15 at 16:00

9

This is exactly what I was looking for too, the Github Desktop documentation doesn't even mention the feature and this is where I ended up when searching for an answer as to what it does. It may not be "the answer" but it is useful.
– DanielDec 5 '15 at 4:00

1

The git command for deleting a remote branch sucks and I tend to forget it (both new and old). Luckily there are GUI tools that have the option. Git Gui, TortoiseGit and GitHub Desktop have it - I wish Git Extensions had this functionality too. Anyway, what I remember is to start Git Gui from within Git Extensions when I need to delete a remote branch.
– vezenkovMar 25 '16 at 21:59

This is fine, but people really shouldn't be naming their branches and tags with the same name and same naming scheme in the first place.
– user456814Jul 29 '14 at 10:00

2

Well, my scenario was that I was converting a branch to a tag and it made sense for the tag to have the same name as the branch. By converting I mean merging branch B to A and tagging the last commit in branch B with tag B so that after deleting branch B it can still be easily restored by simply checking out tag B.
– AmiramixJul 30 '14 at 11:59

Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need git branch -D branch_to_delete if it's not fully merged into some_other_branch, for example.

Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up to date remotes available on a repository you're tracking. I've observed git fetch will add remotes, not remove them. Here's an example of when git remote prune origin will actually do something:

User A does the steps above. User B would run the following commands to see the most up to date remote branches

@Yar this link is out of the context and have a more broader scope. I tell only about git and as topic is not originated only for OSX, that choose is strange for other systems (e.g. *UNIX, Windows)
– ReishinMay 21 '15 at 20:33

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).