Space Cat, Prince Among Thieves

git gc - You're not going to need it

tl;dr

I recommend every developer turn off automatic garbage collection in git. It's one of the first things I do setting up a new machine - and you should do so too.

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

$ git config --global gc.auto 0

I promise 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!? Eh, not really. Not for you. I'm not being sarcastic. You just probably don't work at Meta or Microsoft scale.

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 freshly 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 roll of the dice.

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

Basically, turning automatic GC off means any amount of history rewriting is always undoable.

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 disappearing 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 safely work on a detached HEAD if you're wild like that. You probably shouldn't do that unless you know exactly what you're doing, but you certainly could.

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.

Basic HTML allowed.