DEV Community

Ben Halpern
Ben Halpern

Posted on

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!

Discussion (18)

Collapse
pandademic profile image
Pandademic • Edited on

We all know I'll never actually remember the command

maybe an alias?
anyway , thanks for the tip!

Collapse
ben profile image
Ben Halpern Author

maybe an alias?

That's reasonable. I have aliases for a lot of my more frequent tasks like this. It's irrational in this case so maybe I should just get over this — but I feel a little nervous about making aliases for anything I use infrequently in case I forget what the underlying execution is.

Collapse
goodevilgenius profile image
Dan Jones

I second using an alias. If you forget the alias you set, you can do git config -l to list all config values, including aliases, and just scan through them.

Or, if you remember the alias name, you can do get config --get alias.alias-name to show what it's an alias of.

Thread Thread
pandademic profile image
Pandademic • Edited on

thanks , I didn't know that... It's useful for more complicated aliases.

Collapse
kenbellows profile image
Ken Bellows • Edited on

... in case I forget what the underlying execution is.

...because you currently remember it so well that you wrote a post for your own future reference? 😛

Collapse
jeremyf profile image
Jeremy Friesen

I wrote local-branches-with-missing-remote which helps me keep my local branches tidy.

I run local-branches-with-missing-remote | xargs git branch -D to remove all local branches that had remote branches, but those remote branches are gone.

So, when I merge my PR and Github deletes the branch, I can run the above command and keep my branch list nice and tidy.

Collapse
jmau111 profile image
jmau111

Pretty cool script!

Collapse
lucassperez profile image
Lucas Perez

One observation to be made is if you for some reason has another branch that has "main" as part of its name, this command would not delete it. Maybe grep -v "^\*\? *main$", but then its getting already too complicated... 😅

Collapse
amirhe profile image
Amir.H Ebrahimi

make an alias out of it which also protect master and dev branches,
and also try to delete them in safer way with -d instead of -D.

alias gitrm='git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d'
Enter fullscreen mode Exit fullscreen mode
Collapse
jmau111 profile image
jmau111

Nice use of pipes 🦄

The only potential "problem" with the capital D option is that it's a force delete (unlike -d), regardless of any merged status or other potential state.

You might end up desynchronizing remote and local repos and lose any change you made but I guess it's the purpose here. May I ask you the exact context to use it? What do you typically do before/after running thing command?

Collapse
rubiin profile image
Rubin

most of my local branches have a prefix. So when i needed to cleanup all the local branches except the prod ,i have an alias set as git_branch_delete="git branch | grep $@ | xargs git branch -D". So i just do git_branch_delete flip and deletes all branches starting with flip prefix

Collapse
chiangs profile image
Stephen E. Chiang • Edited on

You can create an alias function like this:

removeLocals = "!f() { git branch | grep -v "main" | xargs git branch -D; }; f"
Enter fullscreen mode Exit fullscreen mode

used: git removeLocals

or if you want to account for master vs main

removeLocals = "!f() { git branch | grep -v /"$1/" | xargs git branch -D; }; f"
Enter fullscreen mode Exit fullscreen mode

used: git removeLocals main

I recommend not doing the second one as you may accidentally remove your local version of main/master due to a typo

You could also just make two aliases removeLocalsMain and removeLocalsMaster

Collapse
deciduously profile image
Ben Lovy

Every time I successfully use xargs I spend the next several hours thinking "whoa, I did an xargs". It somehow never gets old.

Solid xargs, good stuff.

Collapse
phlash909 profile image
Phil Ashby

Ha, nice :)

For extra coolness, especially when processing files with whitespace, I have trained my muscle memory to use:
find ... -print0 | xargs -0 ...
replacing the whitespace separator with ASCII NUL.

Collapse
lwhiteley profile image
Layton Whiteley

If you want to delete only branches that have been deleted remotely then git fetch --prune is an option

Collapse
jknair0 profile image
Jaya Krishnan Nair

i think instead of main $(git_main_branch) should be helpful

Collapse
harlessmark profile image
Mark Harless

That's a great tip! Thanks!

Collapse
serhatteker profile image
Serhat Teker

Nice. Just one thing; it only deletes local ones.