How do I delete a git branch both locally and remote?
52
I created a bugfix branch to fix a bug on a project that I had forked on GitHub. I issued a pull request to the developer to incorporate my fix, but the developer decided to implement a different fix for the problem. At this point, I want to delete the bugfix branch both locally and on my project fork on GitHub.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub? 
improve this question | comment
Russell Hane Created at: 2013-11-13 17:07:51 UTC By Russell Hane
11 Answers
1
From http://yeppudaproductions.wordpress.com/2012/11/16/git-para-preguicosos/

git branch -d <name-of-branch>
git branch -d -r origin/<name-of-branch>
git push origin :<name-of-branch>

0
Tip: When you delete branches using git branch -d <branchname> or git push origin :<branchname> the reference is only deleted locally. This means that for other team members the deleted branches are still visible when they do a git branch -a.

To solve this your team members can prune the deleted branches with git remote prune <repository>, this is typically git remote prune origin.
0
You can also use the following to delete the remote branch
$ git push origin --delete serverfix
which does the same thing as $ git push origin :serverfix but may be easier to remember.
0
Another approach is git push --prune origin. This will delete all remote branches that do not exist locally.

More comprehensively, git push --mirror  will effectively make the remote look like the local copy of the repo (local heads, remotes and tags are mirrored on remote).
0
I use the following in my bash settings:

alias git-shoot="git push origin --delete"

Then you can call:

git-shoot branchname
0
Matthew's answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:

To remove a local branch from your machine:

git branch -d the_local_branch

To remove a remote branch:

git push origin :the_remote_branch

*Taken from here.
0
If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your ~/.gitconfig:

[alias]
    rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"


Alternatively, you can add this to your global config from the command line using

git config --global alias.rmbranch '!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'


NOTE: If using -d (lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use -D (uppercase D).
0
In addition to the other answers, I often use the git_remote_branch tool. It's an extra install, but it gets you a convenient way to interact with remote branches. In this case, to delete:

grb delete branch


I find that I also use the publish and track commands quite often.
0
Since January 2013 Github included a "delete branch" button next to each branch in your "branches" page: https://github.com/blog/1377-create-and-delete-branches
0
You can also do this git remote prune origin

$ git remote prune origin
Pruning origin
URL: git@example.com/yourrepo.git
 * [pruned] origin/some-branchs


prune delete remote-tracking branches from git branch -r listing
0
Updated Answer on 1-Feb-2012

As of Git v1.7.0, you can delete a remote branch using

git push origin --delete <branchName>


which is easier to remember than

git push origin :<branchName>


which was added in Git v1.5.0 "to delete a remote branch or a tag."

Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.

Original Answer from 5-Jan-2010

From Chapter 3 of Pro Git by Scott Chacon:


  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 codeline is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]. If you want to delete your serverfix branch from the server, you run the following:

$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
 - [deleted]         serverfix

  
  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).
Your Answer