Q: What is git gc? What does git garbage collection actually do under the hood, and when should you run it manually vs relying on auto-gc?
Deep dive into git gc: compressing loose objects into packfiles, repacking, pruning unreachable commits beyond expire thresholds, and optimizing bloated Git repositories.
Want to master this scenario in a live sandbox? KodeKloud's Advanced Git Workflows, Rebase & Interactive Diagnostics covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "In a fast-paced team with dozens of pull requests merged daily, Git workflow hygiene was critical. The interviewer is testing: Understanding of Git's garbage collection and object packing.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Using deprecated git filter-branch rather than modern git filter-repo to scrub committed credentials
git filter-branchis slow, error-prone, and frequently leaves dangling blobs, whereasgit filter-repocleanly purges sensitive files. - Pushing large compiled binary artifacts, disk images, or zip packages into standard Git repository history Git tracks every binary version permanently; bloating repository size slows git clone and fetch operations across all team members.
- Rebasing or amending commits that have already been published and merged into upstream main branches Rewriting published history forces all collaborators to perform complex manual reconciliations and corrupts continuous delivery tracking.
- Failing to rotate leaked credentials immediately under the false assumption that rewriting Git history revokes access in git-q045-what-does-git-gc-do-and-when-should Once pushed to a remote repository, secrets must be treated as fully compromised regardless of subsequent git history rewriting.