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.
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.
Practice
A real repo, pre-seeded with history, runs right here — stage, commit, branch, stash, diff, even hit and resolve a real merge conflict. Nothing to install, nothing leaves your machine.
Open practice playground