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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s