Q: What does `git gc` do, and when should you run it manually?
git gc (garbage collection) performs housekeeping on the Git object database:
🛠️ Production Runbook & Step-by-Step Resolution
Initial Diagnostics & Root Cause Analysis
git gc (garbage collection) performs housekeeping on the Git object database:
- Packs loose objects into packfiles — individual objects in
.git/objects/are compressed into efficient.packfiles with delta compression. - Removes unreachable objects — commits, blobs, and trees that no ref (branch, tag, reflog) points to. These are typically from amended commits, rebases, or deleted branches.
- Packs refs — consolidates individual ref files in
.git/refs/into a singlepacked-refsfile. - Prunes old reflog entries — entries older than 90 days (reachable) or 30 days (unreachable) by default.
Remediation & Permanent Safeguards
When Git runs it automatically: Git triggers gc --auto after certain operations (e.g., when there are more than 6700 loose objects or more than 50 packfiles). You rarely need to run it manually. When to run manually: Aggressive GC: This spends more CPU time on delta compression for a smaller packfile. Only useful occasionally (e.g., after importing from another VCS). Don't run it routinely — the default compression is good enough and --aggressive is slow. Caution: --prune=now immediately deletes unreachable objects. If you're recovering lost commits via reflog, run recovery *before* GC.
git gc --aggressive --prune=now
- After large history rewrites (
filter-repo, mass deletions) to reclaim disk space. - When the repo feels slow and
.git/is unusually large. - Before creating a bundle or archive — ensures maximum compression.
- Packs loose objects into packfiles — individual objects in .git/objects/ are compressed into effi...
- Removes unreachable objects — commits, blobs, and trees that no ref (branch, tag, reflog) points ...
- Packs refs — consolidates individual ref files in .git/refs/ into a single packed-refs file.