git delete all local branches except some

Sometimes after a sprint, all the remaining branches are just taking up space.
Here’s a small snippet to remove all your local branches in one go.

$ git branch | grep -v "master" | xargs git branch -D 

grep -v "master" # will exclude the `master` branch

For an easier reuse you could also alias this useful snippet.

$ echo 'alias gbr="git branch | grep -v "master" | xargs git branch -D"' >> ~/.bash_aliases

Now you only need to type `gbr` to delete all the local branches except `master` 

>>‘ will append the file with the line above

2 thoughts on “git delete all local branches except some

Leave a comment