DEV Community

Cover image for Clean local GIT branches
Maksim Surguy
Maksim Surguy

Posted on

1 3

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more