How do I delete a Git branch both locally and remotely?

+11591 votes
asked Jan 5, 2010 by matthew-rankin

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

Failed 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?

29 Answers

+14351 votes
answered Jan 5, 2010 by matthew-rankin

Executive Summary

$ git push -d origin <branch_name>
$ git branch -d <branch_name>

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 [Updated on 8-Sep-2017]

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

$ git push origin --delete <branch_name>

which might be easier to remember than

$ git push origin :<branch_name>

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

Starting on Git v2.8.0 you can also use git push with the -d option as an alias for --delete.

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

Delete Remote Branch [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).

Then you should execute this on other machines

git fetch --all --prune

to propagate changes.

+575 votes
answered Oct 27, 2011 by pagetribe

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 it may be easier to remember.

+80 votes
answered Mar 24, 2012 by u2622

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.

+2467 votes
answered Jun 12, 2012 by eric-brotto

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} (use -D instead to force deletion without checking merged status)

To remove a remote branch from the server:

git push origin --delete {the_remote_branch}

Reference: https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote

+295 votes
answered Nov 7, 2012 by pfrenssen

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.

+160 votes
answered Nov 18, 2012 by imanuelcostigan

Another approach is

git push --prune origin

WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,

git push --mirror

will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).

+95 votes
answered Jan 15, 2013 by ryan-kohn

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

+84 votes
answered Mar 12, 2013 by nickleefly

You can also do this using git remote prune origin:

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

It prunes and deletes remote-tracking branches from a git branch -r listing.

+135 votes
answered Apr 2, 2013 by crizcraig

I use the following in my Bash settings:

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

Then you can call:

git-shoot branchname
+104 votes
answered Apr 29, 2013 by nacho-coloma

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

Relevant blog post: Create and delete branches

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...