Normally when you use GIT for a long time, you end up with a huge list of branches in both local and remote repository. In this post I explain how you can get rid of that clutter.
Deleting branches in the local repository
I assume you know how to list local branches in the repo (Simply use
). There are couple commands you can use to delete a
local branch. Following command is the safe one to use when deleting
local branches. It prevents users accidentally deleting a branch which
is not fully merged in its upstream branch, or not in HEAD if no
upstream was set with –track or –set-upstream.1
git branch
You will get the following error if you try to delete a branch which is not fully merged.
In that case you can use
option to force branch deletion.1
-D
Deleting branches in remote repository
There are couple git commands you can use to delete a git branch in a remote repository. Please note that the following examples assume the remote name as “origin”. After Git v1.7.0 you can use the `–delete’ option with git push to delete a remote branch.
If you are using an older version you can use the command mentioned below.
Both the commands have the same effect. Therefore you can use the one you like. For me the first command feels more natural.
Creating a Git alias to delete both local and remote branches
You can create a Git alias if you find yourself executing these two commands together most of the time.
Now it is simple as issuing following command to delete a branch from both local and remote repositories.