Delete a Git branch both locally and remotely

Question:

Original question:Delete a Git branch both locally and remotely from Matthew Rankin

I want to delete a branch both locally and on the fork of my remote project on Github.

Removed local branch successfully

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

Note: The -D option is a shortcut to --delete --force . If you want to remove the local branch that must be fully merged into the upper branches, use -d which is a shortcut to -delete .

Failed attempts to delete a remote fork

$ 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 remove the remotes / origin / bugfix branch both locally and on Github?

Answer:

Original answer: Delete a Git branch both locally and remotely by Matthew Rankin

Answer updated 1-Feb-2012

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

git push origin --delete <NombreRama>

which is easier to remember than

git push origin :<NombreRama>

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

Therefore, the version of git you have installed will dictate whether you need to use the easier or more difficult syntax.

Original answer from 5-Jan-2010

From Scott Chacon's Pro Git Chapter 3:

Delete remote branches

Suppose you have terminated a remote branch – you and your collaborators have terminated a feature and merged it into your remote master branches (or whatever branch your stable code is on). You can remove a remote branch by using git push [NombreRemoto] :[Rama] instead of the obtuse syntax. If you want to remove your serverfix branch from the server, do the following:

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

You no longer have the branch on your server.

Since there is a strong possibility that you forget the syntax, you may want to remember this command later. One way to remember this command is by calling the syntax git push [NombreRemoto][RamaLocal]:[RamaRemota] that we mentioned earlier. If you don't put the [localbranch] , then you're basically saying, "Take nothing on my part and do it [remotebranch] ."

I used git push origin :bugfix and it worked great. Scott Chacon was right – I would bookmark that page (or do it here, replying on Stack Overflow).

Scroll to Top