Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to delete a branch both locally and on my remote project fork on GitHub.

Successfully Deleted Local Branch

$ git branch -D bugfix
Deleted branch bugfix (was 2a14ef7).

Attempts to Delete Remote Branch

$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.

$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.

$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push
Everything up-to-date

$ git pull
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.

What do I need to do differently to successfully delete the remotes/origin/bugfix branch both locally and on GitHub?

share|improve this question

This question has an open bounty worth +200 reputation from Cupcake ending in 17 hours.

This question has not received enough attention.

Note that many of the answers here miss a certain important point: the reason that simply using git branch -rd origin/<branch> won't delete the branch on the remote repo is because origin/<branch> is a local remote-tracking branch. I discuss this important distinction between remote branches and remote-tracking branches in my own answer.

8  
git push origin :branchName for remote git branch -d your branchname for local –  Ramesh Rajendran Feb 27 at 15:50
16  
Moderator note: If you intend to answer this question, do note that there are already 24 answers posted. Will your new answer add any substantial value? –  Robert Harvey Jun 11 at 16:10

13 Answers 13

up vote 4079 down vote accepted

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

share|improve this answer
    
what's that serverfix? @TrentonScott –  gumuruh Feb 1 '12 at 7:22
84  
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é Lafortune May 11 '12 at 4:05
16  
You may also want to follow up with git remote prune origin, right? –  Jesse Glick Jul 24 '12 at 15:36
6  
@jesse-glick According to the manual In most cases, users should run git gc, which calls git prune. –  Michael G. Emmons Oct 20 '12 at 17:15
3  
Note that this will not delete your local copy of the branch. Try it, do a git branch after issuing the delete. The branch is still there. To remove it locally you can issue git branch -d local_branch. Cleaning up local references via git gc might possibly work as well, but I haven't tried it. –  Michael G. Emmons Oct 24 '12 at 15:09

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: https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote

share|improve this answer
6  
This was just what I needed. The 'remote branch' command gave me: error: unable to push to unqualified destination: name The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. (in case anyone googles this) –  Thomas Ahle Sep 18 '12 at 21:56
    
@ThomasAhle I am getting the same error with git push origin --delete <remote_branch_name> Did you figure out what the problem was? –  user815423426 Oct 3 '12 at 15:51
4  
we can also use git branch -D the_local_branch –  vajapravin Feb 19 '13 at 7:55
10  
@vajapravin keep in mind that that has slightly different behaviour, it deletes the branch even if not merged. –  Dennis Aug 31 '13 at 5:41
    
i think it should b '-D' instead of '-d' in "git branch -d the_local_branch" command above –  Shiri Hrw May 26 at 9:24
up vote 112 down vote
+100

You can also use the following to delete the remote branch

git push --delete origin serverfix

which does the same thing as

git push origin :serverfix

but may be easier to remember.

share|improve this answer

Tip: When you delete branches using

git branch -d <branchname>    # deletes local branch

or

git push origin :<branchname> # deletes remote branch

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.

To solve this your team members can prune the deleted branches with

git remote prune <repository>

this is typically git remote prune origin.

share|improve this answer
7  
You should clarify that the above git push operation deletes the local branch and the remote branch. –  Adam Backstrom May 21 '13 at 13:51
3  
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. –  Cupcake Jun 11 at 16:30

The Short Answers

Deleting a remote branch:

git push origin --delete <branch>  # Git version 1.7.0 or newer
git push origin :<branch>          # Git versions older than 1.7.0

Deleting a local branch:

git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force delete un-merged branches

The Long Answers: there are 3 different branches to delete!

When you're dealing with deleting branches both locally and remotely, keep in mind that there are 3 different branches involved:

  1. The local branch X.
  2. The remote origin branch X.
  3. The local remote-tracking branch origin/X that tracks the remote branch X.

Visualization of 3 branches

The original poster used

git branch -rd origin/bugfix

which only deleted his local remote-tracking branch origin/bugfix, and not the actual remote branch bugfix on origin.

enter image description here

To delete that actual remote branch, you need

git push origin --delete bugfix

The following sections are the detailed steps needed to actually delete these various branches.

Deleting the local branch X

git branch --delete X

# Or shorter
git branch -d X

# If X hasn't been merged yet, you'll need to force delete
git branch -D X

Deleting the remote branch X

You can use either of the following to delete the remote branch (depending on what version of Git you're using):

# Use an empty refspec to delete the remote branch.
# This will work in versions of Git older than 1.7.0.
git push origin :X

# If you're using Git version 1.7.0 or newer
# you can use the new --delete flag instead.
git push origin --delete X

Note that deleting the remote branch X from the command line this way will also delete the local remote-tracking branch origin/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:

# View just remote-tracking branches
git branch --remotes
git branch -r

# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a

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 origin --prune

# Or shorter
git fetch origin -p

Here is the relevant quote from the 1.6.6 release notes (emphasis mine):

"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).

Alternative to above automatic pruning for obsolete remote-tracking branches

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:

git branch --delete --remotes origin/X

# Or shorter
git branch -dr origin/X

See Also

share|improve this answer

Another approach is

git push --prune origin

This will delete all remote branches that do not exist locally. Or 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).

share|improve this answer
1  
git push --prune doesn't appear to exist in version 1.7.9.msysgit.0 (which is what I appear to be running). –  Owen Blacker Jan 29 '13 at 19:28
1  
--mirror works great in case several local "branch deletes" need to be synced up with the origin. –  tolitius Feb 13 '13 at 0:12
46  
git push --prune origin looks dangerous... –  Nate Jun 28 '13 at 22:37
1  
It's infinitely safer to do git fetch -p or git fetch --prune then on a push. You never know when someone else's config will end up pushing OTHER things in the process.. and if you typo the -p or --prune you don't push by accident :-) –  UpAndAdam Oct 16 '13 at 14:16

I use the following in my bash settings:

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

Then you can call:

git-shoot branchname
share|improve this answer
69  
reminds me alias please="sudo" –  Mohsen May 15 '13 at 17:56
47  
You can also use a git alias instead of a bash alias. Add shoot = push origin --delete to your ~/.gitconfig and you will be able to use git shoot branch. –  Nils Werner Jun 10 '13 at 9:36

Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.

Relevant blog post: Create and delete branches

share|improve this answer
1  
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 Jackson Sep 11 '13 at 12:18
1  
I was going to point this one out. Note that the button won't delete your local branch... see this answer for how to do that: stackoverflow.com/a/10999165/901641 –  ArtOfWarfare Oct 29 '13 at 14:02

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

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete

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 repo 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

git fetch
git remote prune origin
git branch -r
share|improve this answer
    
Users B commands works to remove remote deleted branches.. –  Morten Holmgaard Feb 4 at 10:22

Mashup of all the other answers. Requires Ruby 1.9.3+, tested only on OS X.

Call this file git-remove, make it executable, and put it in your path. Then use, for example, git remove temp.

#!/usr/bin/env ruby
require 'io/console'

if __FILE__ == $0
      branch_name = ARGV[0] if (ARGV[0])
      print "Press Y to force delete local and remote branch #{branch_name}..."
    response = STDIN.getch
    if ['Y', 'y', 'yes'].include?(response)
      puts "\nContinuing."
      `git branch -D #{branch_name}`
      `git branch -D -r origin/#{branch_name}`
      `git push origin --delete #{branch_name}` 
    else
      puts "\nQuitting."
    end
end
share|improve this answer
1  
one-letter get courtesy of this answer: stackoverflow.com/a/8072675/8047 –  Yar Nov 19 '13 at 21:04
    
also check this out, which I have not (yet): github.com/webmat/git_remote_branch –  Yar Nov 20 '13 at 20:00

protected by Community Jan 20 '13 at 21:48

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.