Quick tip: Deleting merged branches

If you are working with feature branches, a common approach is to delete the feature branch after it has been merged back to your main development branch. This helps to keep your project tidy and organized. Here are two short bash snippets to delete merged branches.

Deleting local branches that have been merged:

git branch --merged | egrep -v "(^\*|master|develop|release)" | xargs git branch -d  

Deleting remote branches that have been merged:

git branch -r --merged | egrep -v "(^\*|master|develop|release)" | xargs -L1 | cut -d "/" -f2- | xargs git push origin --delete  

Both commands will skip master, develop and release/* branches.

Disclaimer: Please note that these come without any warranty. You should never blindly copy-paste terminal commands. Also, YMMV.

Erik van der Wal

Erik van der Wal

I love building things with Swift, Objective-C, Ruby and tinkering with technologies like Golang and Elixir.

  • The Netherlands
comments powered by Disqus