Last Updated: August 22, 2023
·
450.5K
· aalvarado

Remove all your local git branches but keep master

Sometimes after a sprint, all the remaining branches are just taking up space.
Here's a small snippet to remove all your local branches in one go.

$ git branch | grep -v "master" | xargs git branch -D 

15 Responses
Add your response

For an easier reuse you could also alias this useful snippet.

alias gbr="git branch | grep -v "master" | xargs git branch -D"
over 1 year ago ·

Sill question, maybe, but why not just work with 2, 3 or 4 if you really must branches? I can imagine large teams... very large teams - of maybe 15 - 30+ developers would have many branches to be reconciled, but this post states distinctly local work (so I assume, it's on one man's machine, or am I misinterpreting?)

Nevertheless, I'm interested in how different people code in their own ways. Personally I may have master, development, and if I really must, a "messing around" branch. And no more.

I'm aware that you may be referring to exactly the way I do it - are you? In that case, this would be to clean up your dev branches from local after checking out, is that right?

over 1 year ago ·

@skopp
Not a silly question at all. It is interesting to know how other people deal with complexity and how teams manage branches. I've worked on 2 different companies since I've used Git to manage development for projects. This guide over here: https://github.com/diaspora/diaspora/wiki/Git-Workflow it is essentially what we do.

In short, we create a develpment branch for every issue/bug we want to work on, and then merge into master. Most if not all the time, we simply forget or don't care to remove those development branches from our local repository, and thus can take up hard drive space. Also; it is useless to keep a 3 month old branch if it has been merged to master or no longer relevant.

There's a post from my friend here: https://coderwall.com/p/ssm_zg?i=7&p=1&q=author%3AEnriqueVidal&t[]=EnriqueVidal that lets you remove remote branches that have been merged to master, so you can keep things neat on the remote side as well.

Tl;dr version:

Sill question, maybe, but why not just work with 2, 3 or 4 if you really must branches?

This could work for a solo project, but on a 2+ man team, this would not work as well.

I can imagine large teams... very large teams - of maybe 15 - 30+ developers would have many branches to be reconciled, but this post states distinctly local work (so I assume, it's on one man's machine, or am I misinterpreting?

Not misenterpreting, rather we just have a different workflow, ( a branch per issue if you would like to think at it like that).

Nevertheless, I'm interested in how different people code in their own ways. Personally I may have master, development, and if I really must, a "messing around" branch. And no more.

In all of my 1 man projects I do it like that, if there's a team ( >1 ), I like to do the "diaspora" workflow I linked to.

I'm aware that you may be referring to exactly the way I do it - are you? In that case, this would be to clean up your dev branches from local after checking out, is that right?

I usually never need to clean up on my 1 man projects, on the others I do it around every 3 or 4 months.

Cheers!

over 1 year ago ·

@aalvarado thanks a ton for all that information - skimmed, but will def. give it all a thorough read a bit later!

over 1 year ago ·

If you have branches containing master then this script won't delete those.

Instead you could use:

git branch | grep -ve " master$" | xargs git branch -D

This will remove branches named (e.g.) test-master, master-test.

over 1 year ago ·

@fiznool I was thinking of that myself today, thanks! I solved it with

ack -v "\s+master"
over 1 year ago ·

more than 1 branch can be added to the grep expression like "master\|develop\|current_branch"

over 1 year ago ·

Another super useful one is
git config --global alias.clean-branches "!git branch | grep -v master | xargs git branch -D"

I like having it be available as a git alias, it makes the workflow a little nicer, since you'll probably already be doing git things

over 1 year ago ·

I use

alias gbDA='git branch | egrep -v "(master|\*)" | xargs git branch -D'

Which deletes everything except master and the branch I am currently in, just in case.

over 1 year ago ·

this snippet will not remove the current branch.

over 1 year ago ·

It won't, but it will show an unsightly error message because it unsuccessfully tried to delete the current branch.

over 1 year ago ·

git branch | select-string -notmatch master | foreach {git branch -d ("$_").Trim() --force}

over 1 year ago ·

Thank you Adan Alvarado

over 1 year ago ·

I needed multiple branches excluded and found it somewhat difficult to actually do, but here's what finally worked for me:
1) Go find your actual global git config file and edit that. Trying to do it via command line was too hard with escaping and such
2) Make it look like this:
[alias]
trim = !git branch | grep -v -E 'master|main|development|integration|production' | xargs git branch -D
Should go without saying that each term after -E are the branches that WILL NOT be deleted.

over 1 year ago ·

Okay, easy, thanks.

over 1 year ago ·