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
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)
maybe an alias?
anyway , thanks for the tip!
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.
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.thanks , I didn't know that... It's useful for more complicated aliases.
...because you currently remember it so well that you wrote a post for your own future reference? π
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.
Pretty cool script!
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... π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.
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?
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 dogit_branch_delete flip
and deletes all branches starting withflip
prefixYou can create an alias function like this:
used:
git removeLocals
or if you want to account for master vs main
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
Every time I successfully use
xargs
I spend the next several hours thinking "whoa, I did anxargs
". It somehow never gets old.Solid
xargs
, good stuff.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.
If you want to delete only branches that have been deleted remotely then
git fetch --prune
is an optioni think instead of main
$(git_main_branch)
should be helpfulThat's a great tip! Thanks!
Nice. Just one thing; it only deletes local ones.