DEV Community

Cover image for Clean local GIT branches
Maksim Surguy
Maksim Surguy

Posted on

Clean local GIT branches

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:

"master\|staging\|dev"

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.

Enjoy!

Top comments (0)