DEV Community

Cover image for Git basics: remove all local branches

Git basics: remove all local branches

Chris Bongers on July 12, 2022

There will come a time when you optimize your workflow to ensure PR requests are as small as possible. Each request can be minimal, so you end up w...
Collapse
 
damian_cyrus profile image
Damian Cyrus

Very handy one-liners.

There should be a git command especially for this cases. Something like:

git branch --all-branches --merged --delete --ignore="fix-this-bug,prefix-*"
Enter fullscreen mode Exit fullscreen mode

And the main branch is ignored by default, to not do something dangerous. Maybe also a hint if you delete a branch when you are checkout into it at the moment ...

Or some similar feature, with a good described documentation in one place πŸ˜„

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard

Git is a goldmine for content creators.

So many common tasks don't have an intuitive solution, and the official documentation has a chicken and egg problem where you can search only what you already know how to do.

So lots of Google traffic.

Collapse
 
dailydevtips1 profile image
Chris Bongers

There should really be a really good cleanup way actually.
Heck, even simply an automated way, merged branch -> get info back to delete it when it's not active anymore.

But at the very least indeed a option to make it easier and safe for people to do this.

Collapse
 
yawaramin profile image
Yawar Amin

There will come a time when you optimize your workflow to ensure PR requests are as small as possible.
Each request can be minimal, so you end up with tons of new local branches that have probably already been merged.

When this starts happening at scale, this is probably an indication that we need to think at a higher level of abstraction than 'branches' and 'pull requests', or at least we need a tool that simplifies part of this workflow. Lately I am trying out Git Patch Stack for this. It creates a branch and a PR automatically from a single commit. At this level of abstraction it calls the combination of the branch and PR a 'patch'. You can create and layer multiple patches in a stack, the intention is that each patch gets sent as a PR to be reviewed. As patches are reviewed and merged, the patches above those ones are automatically rebased on top of the new branch head.

It takes a little setup though–the tool by itself doesn't know how to create PRs, it relies on shell scripts called 'hooks' to do that. I am trying it out now but haven't yet made up my mind about whether it's the ultimate solution to the problem. I just think a solution above the level of branches is needed.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks for that vision Yawar.
At what large scale are you working at, just interested to hear :)

Collapse
 
owieth profile image
Oli

Maybe this command is helpful to someone ;)
The command deletes all local branches without an origin attached

git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks for sharing Oli

Collapse
 
himanshupal0001 profile image
Himanshupal0001

Hi Chris, thanks for the post. Could you create series regarding git and git bash. I am struggling with git commands. Mostly when i've to work on already present repositeries. Could you make series how to use git and it's commands affectvily both for new repos and existed repos. Thanks in advance.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Hey,

I actually already did a whole series on Git, this one was kind of the aftermath.
You can find the full series here:
daily-dev-tips.com/tags/git/

Collapse
 
adamisheff profile image
adamISheff • Edited

Love these. I would suggest a mention of how to create an alias for these commands to make using them more intuitive(a challenge with git other comments point out). That is how I've set up similar commands for my shell.

example:
alias deleteLocalBranches="git branch | grep -v "main" | xargs git branch -D"

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yeah, wrote the alias on my todo list to refactor.
If you do have some time before me, feel free to modify it in my repo already.

github.com/rebelchris/daily-dev-ti...

Collapse
 
renanfranca profile image
Renan Franca

Thanks ❀️! That's a useful command! I used to do git prune than going to delete the highlights branches in red by vscode πŸ€“!

Collapse
 
nachokai profile image
Nacho

With Oh My Zsh you can use the alias "gbda" :)
(git branch --no-color --merged | command grep -vE "^(+||\s($(git_main_branch)|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh dang! TIL πŸ˜…

Collapse
 
xapuu profile image
Xapuu

Clone the repo again ;)

Collapse
 
noviicee profile image
Anamika

I used to do this πŸ˜‚ πŸ€“

Collapse
 
dailydevtips1 profile image
Chris Bongers

Also good

Collapse
 
dailydevtips1 profile image
Chris Bongers

Nice! Love it πŸ™
Actually if you want to can add it to my repo?
Would be great for other people too.
github.com/rebelchris/daily-dev-ti...

Collapse
 
sylwiavargas profile image
Sylwia Vargas

I literally went blank about how to do it earlier today (and then got distracted so never googled it). Thank you!

Collapse
 
dailydevtips1 profile image
Chris Bongers

Glad it helped :)

Collapse
 
noviicee profile image
Anamika

Very useful.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks Anamika