git gc
intermediateSweep unreachable objects and compress your repository.
What it does
git gc is Git's garbage collector. It deletes objects with no refs pointing to them (once they're old enough), repacks loose objects into efficient packfiles, and prunes the stale reflog. It shrinks .git and speeds up every subsequent operation.
content bytes…
SHA-1 → unique key of content
git gc collapses loose objects into one packed, delta-compressed packfile
Watch it — press play
A narrated, step-by-step walkthrough that shows how to do it — not just theory. It adapts to the learner mode you picked in the top bar.
Step 1
The repository gets fat
Every add and commit writes loose, individually-compressed files. Thousands later, .git bloats and slows.
git gc --aggressiveLoose objects pile up as you work
Under the hood
Reachability rules
An object is alive if it can be reached from a ref: branches, tags, the index, or the reflog. Anything else is a candidate for pruning — but only if it's aged past gc.reflogExpire (default 90 days).
objects ──reachable via──> refs/heads/*, refs/tags/*, reflog, index, HEAD ↓ unreachable & old pruned by git gc
Loose objects → packfiles
Loose objects are compressed individually (cheap to write, slow to read). git gc repacks them into a single packfile with delta compression — storing only the differences between similar blobs. Massive space savings.
It runs automatically
Git auto-runs a light gc whenever it senses too many loose objects (about 7,000). You rarely need to invoke it by hand — but --aggressive helps a bloated repo.
Aliases the pros type
Memorize the git gc concept, then let one of these shortcuts make it instant. Aliases are real git config keys — add one with:
git config --global alias.st # → git gc --aggressiveg gcagit gc --aggressiveThe full clean, when .git has become bloated.
g gc --prune=nowgit gc --prune=nowForce-remove recoverable-but-really-dead commits.
Command anatomy
--aggressiveMore thorough — find better deltas at the cost of time.
--prune=<date>Prune unreachable objects older than a date or in 'now'.
--autoRun the automatic routine Git uses internally.
When to reach for it
A giant monorepo clone hanging → git gc --aggressive after fixing .gitignore.
You force-pushed away a big binary at a random commit → gc finally removes it from .git.
Sending a repo by zip: pack everything first with gc so the bundle is small.
Pro tip
Before gc, check what's truly dead: `git fsck --lost-found` lists dangling commits (unreachable but not yet pruned). You can watch a commit you 'deleted' linger there peacefully until gc reaps it.