⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Git & Branching Workflows Interview Questions Scenario 45 of 52 in Git & Branching Workflows
Senior DevOps / SRE [L2] Git Amend without editing message Production Scenario [L2]
🎯 Target Role / Context: Git Scenario #45 (L2) · Amend without editing message

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.

#Git #L2 #Version Control #Collaboration #Rebase #what is git gc #git gc #git prune #packfiles #loose objects #repository maintenance
🎙️ Candidate Opening & Architectural Context
""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.""
Advertisement
⚡ Recommended Practice Lab

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."

1️⃣

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 .pack files 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 single packed-refs file.
  • Prunes old reflog entries — entries older than 90 days (reachable) or 30 days (unreachable) by default.
2️⃣

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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Packs loose objects into packfiles — individual objects in .git/objects/ are compressed into efficient .pack files with delta comp."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Using deprecated git filter-branch rather than modern git filter-repo to scrub committed credentials git filter-branch is slow, error-prone, and frequently leaves dangling blobs, whereas git filter-repo cleanly purges sensitive files.
  2. 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.
  3. 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.
  4. 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.
Advertisement
Want more Git & Branching Workflows scenarios?
Explore our complete collection of scenario-based Git & Branching Workflows interview runbooks.
Browse All Git & Branching Workflows Questions →