First of all, you must to be updated with your remote.
After that, the command is:
git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 -I {} git branch -d {}
Explanation:
git branch --merged
: list only branches merged intoHEAD
of current branchgrep -v "\*"
: remove the current branch from the list (we do not/can not remove it)grep -v master
: removemaster
branch from the listgrep -v dev
: removedev*
branch from the list (useful if you are working with GitFlow ordevelop
branch)xargs -n 1 -I {} git branch -d {}
: we will remove branch per branch (because we set the-n
to1
).
If you want to list all branches that this command will delete, remove the last part:
git branch --merged | grep -v "\*" | grep -v master | grep -v dev
Originally created in @artefactop 's Gist