Menu

Git: How to remove merged branches locally

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. Also Git is a very powerful tool.

Git supports branches. And branches in git cost almost nothing. As a result when we create a new feature, we create a "feature branch", work there. When a feature is complete the "feature branch" is merged to development or master branch. After that feature branch could be deleted because we don't need it anymore.
To delete branch locally we can use the following command into command line (console, terminal):

git branch -d feature_branch_name

But it could happen that a lot of merged branches stayed in our local git repository. To find and delete all merged branches, except master and development, we can use the following command in our command prompt (console, terminal):

The first part of the command git branch --merged | egrep -v "(^\*|master|development)" will find all branches which are already merged. The second part of the command xargs git branch -d will delete all found branches.

The mentioned approach will work well only on Unix/Linux OS. Unfortunately Windows doesn't have egrep utility.

Fortunately we can use it in Windows from Git Bash. Git Bash is a console (terminal) for Windows which supports bash with some limitations. Git Bash is included into official Git installer for Windows.

By the way, to delete remote branch we could use the following command: