Alias To Quickly Remove Merged Git Branches

July 10, 2018

I’ve recently setup an alias to remove all git branches in a repo that have already been merged into master. It’s become part of my daily workflow, so I thought to share it:

gdb='git branch --merged | egrep -v "(^\*|master)" | xargs git branch -d'

It’s gdb and stands for Git Delete Branches

Here it is in action:

❯ git branch
  feature/BACK-507-rg-sg1-m
  feature/BACK-508-rg-sg1-n
  feature/BACK-512-rg-sg1-r
  feature/BACK-513-rg-sg1-s
* master
❯ gdb
Deleted branch feature/BACK-507-rg-sg1-m (was 411d530).
Deleted branch feature/BACK-512-rg-sg1-r (was 0dc7159).
Deleted branch feature/BACK-513-rg-sg1-s (was 01ab717).
❯ git branch
  feature/BACK-508-rg-sg1-n
* master
❯

feature/BACK-508-rg-sg1-n hadn’t been merged into master yet, so it remained.

Add the alias to your .bashrc (linux), .bash_profile (mac), or .zshrc (either O/S).

If you’re not sure how to add an alias, let me google that for you.


Enjoy!