Space Cat, Prince Among Thieves

git gc - You're not going to need it

tl;dr

I want to make a recommendation that every developer turn off automatic garbage collection in git. It's one of the first things I do setting up a new machine.

For a happier life, go to your terminal right now, and run:

$ git config --global gc.auto 0

You won't regret it.

The full story

Turn off garbage collection? Surely, I must be crazy, right? Automatic garbage collection is important, right, they wouldn't turn it on by default if it wasn't!? Not really.

Garbage collection for git in most every use case just isn't worth it. That is unless you're very regularly committing very large files, and then orphaning those commits. I don't see that happening in most uses cases; garbage just doesn't build up that quickly in normal usage.

Don't believe me?

I have been working with the same git repo every day for ten solid years with gc turned off. My .git folder is currently 554 MB whereas a fresh cloned copy is 112 MB. I could be saving a whole 442 MB - whoop de doo.

I don't know about you, but I'll happily pay 442 MB for 10 years of improved data safety.

But why turn it off?

Having it on means detached commits could disappear at any time. It's a lottery roll.

Turning off automatic gc means that anything you once committed will remain restorable, forever. Turning it off means if a rebase goes badly, you can always safely get back to the previous state of a now orphaned commit. There's zero chance of it ever disappearing.

Basically, turning automatic gc off means any amount of history rewriting is always undo-able.

Example

A simple call to git reflog will show you all recent commits, including ones that are no longer on any branch.

$ git reflog
98a9b6b HEAD@{1}: commit (amend): Increase footer font-size and line-height
edc192c HEAD@{2}: commit: Increase footer line height
0360c78 HEAD@{3}: commit: Update footer color to quiet lighthouse
9231f91 HEAD@{4}: commit: Burn down a bunch of old css
…

In the example above, commit edc192c became 98a9b6b when it was amended.

To undo that amend, I can reset the HEAD of my current branch back to edc192c. I have no fear of edc192c ever randomly disappear before I get around to it.


Turning off gc is a safety net that has saved me many times.

Turning off automatic gc means you could literally safely work on a detached HEAD if you're crazy. Probably don't do that unless you know exactly what you're doing.

And of course…

Keep in mind, you can always just run gc manually if you feel like a lot of garbage has piled up.

$ git gc

Comment by: LurkerBoi on

LurkerBoi's Gravatar

This is the one thing I never did until it was too late



Email address will never be publicly visible.

See my Tweet about comment formatting.