How To Delete All Local Git Branches In One Go

Delete all your local branches except the one you specify not to delete

Subscribe to my newsletter and never miss my upcoming articles

Sometimes, you might want to delete all your local branches from a project. Doing it one by one might be very tedious when you have lots of branches.

Thus, with the command below, you can delete all your local branches except main:

git branch | grep -v "main" | xargs git branch -D

Of course, you can replace the main branch with any other branch.

Additionally, you might be interested in Git Aliases. The aliases will improve your experience significantly, and it is going to save you time as well.

Apoorv Tyagi's photo

Thank you so much for writing this. This totally answered my query. I really appreciate it 😃

Ben Hammond's photo

Cool; didn’t know this. I did find out git branch -D can take multiple branches to delete all in one go. But this command you posted is even quicker!

Tracy Nuwagaba's photo

Thanks for sharing