Posts Git remove all local branches except master
Post
Cancel

Git remove all local branches except master

This post has been contributed by my colleague Rudy Adams.

I have a tendency to checkout branches locally for code reviews because intellij’s diffs are much better than gitlabs UI.

Due to this I end up having tons on branches locally that have been merged already and are probably deleted on gitlab (remote).

To get rid of these local branches that have already been merged remotely, I use this command.

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

It deletes all merged branches except master (that’s why the egrep is important, otherwise you’ll lose your local master branch).

Any branches which have not been merged on gitlab will be untouched.

This post is licensed under CC BY 4.0 by the author.