Remove all your local git branches but keep main

ben

Ben Halpern

Posted on April 19, 2022

Remove all your local git branches but keep main

I frequently search for the code to delete all local branches except master so I can copy/paste the result, but I always get the "master" version of this — which doesn't help the copy/paste part of it.

$ git branch | grep -v "main" | xargs git branch -D 
Enter fullscreen mode Exit fullscreen mode

Here's hoping this becomes the top result so I can speed up my process by one second. We all know I'll never actually remember the command.

Happy coding!

💖 💪 🙅 🚩
ben
Ben Halpern

Posted on April 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related