DEV Community

Nicky Marino
Nicky Marino

Posted on • Originally published at nickymarino.com on

How to Remove All Local Git Branches That Aren't 'master'

This post was originally published on my website. Check it out for more awesome content!

Every so often, I’ll want to delete all of my local branches for a repository that aren’t the master branch. An easy command to do this is:

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

Enter fullscreen mode Exit fullscreen mode

(If you want to keep multiple branches, such as master and develop, you can chain them together using grep -v "master\|develop")

git branch lists all of the local branches for the repo, grep -v prints all of the lines from the previous command that don’t match “master”, and xargs takes each line from the previous output and runs git branch -d <output_line>.

I recommend using -d rather than -D in case git recommends not deleting the branch.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay