When working on many feature branches, they tend to accumulate in the local Git clone. Even if they get deleted in upstream shared repos, they need to be cleared locally, too, otherwise they will stick around forever.

Here’s a quick one-liner to clean up every branch that is fully merged to main. It does make sure not to delete main and develop, though.

git branch -d $(git branch --merged main | grep -vE '(^\*|master|main|develop)')

Update (2029-09-22): Let’s make it a git alias!

git config --global alias.prune-merged '!f() { git branch --merged "${1}" | grep -vE "(^\*|master|main|develop)" | xargs git branch -d ; }; f'

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.