Deleting merged local branches in git

When working on a project, the number of branches tend to increase linearly with the work being done. Personally, I don’t like to have all this clutter in my branch list. When I look at the branches, I just want to see the active stuff. A common way to deal with this problem in git is to archive branches. In a previous post I have shown a script that helps you to archive these merged branches automatically and interactively. If this is not an option, because e.g. the conventions of the project don’t allow you to do so, you can alternatively just settle with deleting your local branches. The following script automates that.

Here is an example of the script in action.

$ python ~/dotfiles/delete_local_merged_branches.py -h usage: delete_local_merged_branches.py [-h] [master]

positional arguments: master Name of the master branch. Defaults to 'master'.

optional arguments: -h, --help show this help message and exit

$ python ~/dotfiles/delete_local_merged_branches.py NextMajor Merged local branches: DR-011776_prototype-2 DR-011870 DR-011923 DR-011974

Delete local branch DR-011776_prototype-2? [y/n]y 
Deleted branch DR-011776_prototype-2 (was d814812). 
Delete local branch DR-011870? [y/n]y 
Deleted branch DR-011870 (was efbe881). 
Delete local branch DR-011923? [y/n]y 
Deleted branch DR-011923 (was 1e0d52d). 
Delete local branch DR-011974_add_cppcheck? [y/n]n