DEV Community

Ben Halpern
Ben Halpern Subscriber

Posted on

Git Delete Local Branch How to delete all your local branches but keep master

I find myself searching for this git one-liner a lot, so I figured I'd drop it here to help future searchers:

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

Happy coding ❤️

Top comments (24)

Collapse
 
lesha profile image
lesha 🟨⬛️

Or better, delete everything merged into master, leaving out branches that can contain work in progress

git branch --merged master | grep -v "master" | xargs git branch -D
Collapse
 
thespiciestdev profile image
James Allen

I was waiting for instructions to rm -fr the project and git clone it again 😉 this is much better!

Collapse
 
antonrich profile image
Anton

rm -rf is such a danger. I heard about how dangerous it is, but was oblivious to its power until I deleted a valuable folder!

Collapse
 
vnglst profile image
Koen van Gilst

And on Windows? 😢

Collapse
 
kubadlo profile image
Jakub Leško • Edited

PowerShell in Windows has slightly different syntax, but you can achieve the same result with the line below :)

git branch | Select-String -NotMatch -Pattern "master" | %{ git branch -D $_.ToString().Trim() }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vnglst profile image
Koen van Gilst

Thanks, I should definitely look into Powershell a bit more (first time on a contract for a client with a Windows stack)

Collapse
 
lesha profile image
lesha 🟨⬛️

I mean, this is cool and all, but PS syntax is too explicit imo

Collapse
 
adilfulara profile image
Adil Fulara

Use Linux subsystem 😂

Collapse
 
vnglst profile image
Koen van Gilst

Blocked by the corporate proxy 😢😤

Thread Thread
 
lesha profile image
lesha 🟨⬛️ • Edited

Why though, shouldn't all developers have local admin on workstations?

Or at the very least, shouldn't you be able to request a change in these policies for work-related purposes?

Thread Thread
 
vnglst profile image
Koen van Gilst

currently downloading using the Microsoft store is blocked by the corporate proxy, but maybe there are other ways to get the Linux Subsystem up and running?

Thread Thread
 
david_j_eddy profile image
David J Eddy

Set up a forwarder when at home; use it from the office. :)

Collapse
 
lesha profile image
lesha 🟨⬛️

Git-scm?

Collapse
 
vinayhegde1990 profile image
Vinay Hegde

The feeling you've after the command finishes execution

Like a boss

PS: Don't forget to run git checkout master

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Wow that was perfect as I was deleting about multiple local branches manually!

Left with two and tried that one above and worked fabulously!

demo

Collapse
 
buphmin profile image
buphmin

Just make sure to back up everything first :P

git push --all {remote}

Oh man if I forgot to back up first...that would ruin so many things for me. We have a tendency to re-prioritize things so some branches are left half done for awhile, sometimes many months.

Collapse
 
david_j_eddy profile image
David J Eddy

A handy one indeed @ben . I went one step further and created an alias "git repo cleanup" in my ~/.bash_rc . Keeping a clean local is super helpful when projects get large and dozens of devs have you reviewing / checking / assisting daily.

Collapse
 
khoahuynhdev profile image
Khoa Huỳnh

This is awesome!!!

Collapse
 
chiangs profile image
Stephen Chiang

I was just looking for something like this, thanks!

Collapse
 
chiangs profile image
Stephen Chiang

Here's one if you want to keep both master and develop:

> git branch | grep -v "master\|develop" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ben profile image
Ben Halpern

👌

Collapse
 
itsjzt profile image
Saurabh Sharma

Is there any way to merge everything in master before deleting?

Collapse
 
lesha profile image
lesha 🟨⬛️

It's generally not a good idea. If you finished working on a feature branch, merge it. If a branch isn't merged, it contains work in progress. Work in progress means something isn't working there. Also mass merging often leads to lots of conflicts.

Collapse
 
itsjzt profile image
Saurabh Sharma

Oh thanks