[go: up one dir, main page]

DEV Community

Ben Halpern
Ben Halpern Subscriber

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!

Top comments (18)

Collapse
 
pandademic profile image
Pandademic • Edited

We all know I'll never actually remember the command

maybe an alias?
anyway , thanks for the tip!

Collapse
 
ben profile image
Ben Halpern

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

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

Collapse
 
kenbellows profile image
Ken Bellows • Edited

... 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
 
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
 
spo0q profile image
spO0q

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 Chiang • Edited

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
 
phlash 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
 
spo0q profile image
spO0q

Pretty cool script!

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