Clean local GIT branches

twitter logo github logo ・1 min read

To clean all of your local branches except the current HEAD, master and staging, you can use this script:

git branch | grep -v "master\|staging" | xargs git branch -D 

You can add more branches to the list of the ones that you want to skip, for example if you wanted to keep dev branch in addition to the current branch, master and staging:


To further automate this, you can add an alias to your terminal by modifying your ~/.bash_profile file:

alias clean-branches="git branch | grep -v \"master\|staging\" | xargs git branch -D"

Now, when you're within the project directory, you can use clean-branches command to perform this trimming automatically.


twitter logo DISCUSS
Classic DEV Post from Jan 24

The dangers of the unibrow(ser)

At the beginning of December, Microsoft announced their wish to re-engineer Edg...

Maksim Surguy profile image
I'm a design-savvy full stack engineer who excels at working within multidisciplinary teams. On twitter as @msurguy

Welcome to the DEV Community

Create Your Account