![](/style/images/good.png)
![](/style/images/bad.png)
Remove all your local git branches but keep main
source link: https://dev.to/ben/remove-all-your-local-git-branches-but-keep-main-4e2h
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Posted on Apr 19
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 (15)
Collapse
Expand
We all know I'll never actually remember the command
maybe an alias?
anyway , thanks for the tip!
Collapse
Expand
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
Expand
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
thanks , I didn't know that... It's useful for more complicated aliases.
Collapse
Expand
... 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
Expand
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
Expand
Collapse
Expand
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
Expand
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
Expand
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
Expand
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
Expand
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
Expand
Nice. Just one thing; it only deletes local ones.
Collapse
Expand
i think instead of main $(git_main_branch)
should be helpful
Collapse
Expand
That's a great tip! Thanks!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK