How to delete a Git branch locally and remotely?
Programming Estimated reading time: ~2 minutes
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. In this article you will learn how to delete remote and local branches.
Delete a remote branch (or tag)
Since Git v1.5.0 you can delete a remote branch (or tag) as follows:
$ git push origin :<branch name>
And since Git v1.7.0 you can also delete a remote branch (or tag) by using the following command (which might be easier to remember):
$ git push -d origin <branch name>
And starting with Git v2.8.0,
you can also use git push
with the -d
option as an alias for --delete
.
Delete a local branch
Keep in mind that you will receive an error if you try to delete the currently selected branch. So after you have switched to another branch you can delete a local branch by using the following command:
$ git branch -d <branch name>
The -d
option is an alias for --delete
. The branch will only be deleted if it has
already been fully merged in its upstream branch.
To delete a branch irrespective of its merged status you can use one of the following commands:
$ git branch -D <branch name>
$ git branch -d -f <branch name>
As of Git v2.3,
git branch -d
(delete) learned to honor the -f
(force) flag.
Other usefull commands
Remove obsolete tracking branches after a remote branch has been deleted:
$ git fetch --all --prune
List all available branches:
$ git branch -a
List just the remote branches:
$ git branch -r
A note about Netcup (advertisement)
Netcup is a German hosting company. Netcup offers inexpensive, yet powerfull web hosting packages, KVM-based root servers or dedicated servers for example. Using a coupon code from my Netcup coupon code web app you can even save more money (6$ on your first purchase, 30% off any KVM-based root server, ...).