Q: What is `git fsck` and when would you use it?
git fsck (file system check) verifies the integrity of the Git object database — it walks every object and checks that nothing is corrupt...
#Git #Amend without editing message #L2 #Version Control #Collaboration #Rebase
🎙️ Candidate Opening & Architectural Context
""When an engineer accidentally creates this branch or commit divergence, I walk them through safe recovery without data loss. The interviewer is testing: Knowledge of Git's integrity checking and disaster recovery.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
git fsck (file system check) verifies the integrity of the Git object database — it walks every object and checks that nothing is corrupted, missing, or orphaned.
- Every object's SHA matches its content (detects corruption).
- Every commit's parent exists.
- Every tree's referenced blobs and sub-trees exist.
- No invalid object types or malformed headers.
dangling commit— a commit not reachable from any branch or tag. Often from amended commits, rebases, or deleted branches. Harmless;git gccleans them up.dangling blob— file content not referenced by any tree. Usually from staged-but-uncommitted files.
2️⃣
Remediation & Permanent Safeguards
What it checks: What it reports: When to use it: Recovery from corruption: if fsck reports missing objects, try re-fetching from the remote (git fetch origin). If the remote has the objects, they'll fill the gaps. For truly lost objects, restore from a backup or re-clone.
git fsck --full
missing object— an object referenced but not found. This is actual corruption.broken link— a tree or commit references an object that doesn't exist.- After a disk failure or unclean shutdown — verify the repo isn't corrupted.
- Recovering lost commits —
git fsck --unreachablelists orphaned commits you might want to rescue (alternative toreflog). - Debugging weird Git errors — "fatal: bad object" errors often point to corruption that
fsckcan diagnose.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Every object's SHA matches its content (detects corruption).."
⚡ 60-Second Elevator Pitch Talking Points
- Every object's SHA matches its content (detects corruption).
- Every commit's parent exists.
- Every tree's referenced blobs and sub-trees exist.
Advertisement