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 corrupted, missing, or orphaned.
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: "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."
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.
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.
- Every object's SHA matches its content (detects corruption).
- Every commit's parent exists.
- Every tree's referenced blobs and sub-trees exist.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
-
Running raw git push --force rather than git push --force-with-lease on shared team feature branches when configuring fsck
git push --forceunconditionally overwrites remote commits made by teammates, whereas--force-with-leaseaborts if the remote ref was updated. - Resolving complex merge conflicts with blind checkout overwrite strategies without running unit tests when configuring fsck Unilateral conflict resolution frequently clobbers complementary logic introduced in parallel feature branches, creating regression bugs.
-
Discarding corrupted working directories prematurely instead of recovering lost state using git reflog when configuring fsck
git reflogtracks every HEAD movement, allowing instant recovery of discarded commits, botched rebases, or deleted local branches. - Disabling branch protection rules and mandatory PR approval reviews on shared release branches when configuring fsck Without branch protection, developers can bypass CI validation gates and push untested code directly to production branches.