The Hook (The "Byte-Sized" Intro)
git fsck is a health check for your repository — like running diagnostics on a hard drive. It verifies every object is intact, finds dangling objects nobody references, and reports corruption. But its secret superpower? Finding lost commits and stashes that no branch or reflog points to anymore. It scans the entire object database — your last line of defense.
📖 What is git fsck?
git fsck (file system check) verifies the connectivity and validity of all objects in a Git repository. It reports missing, corrupt, and dangling objects.
Conceptual Clarity
What fsck reports:
| Status | Meaning |
|---|---|
| dangling commit | A commit no branch or tag points to |
| dangling blob | A blob not referenced by any tree |
| dangling tree | A tree not referenced by any commit |
| missing object | An object referenced but not found |
| broken link | A reference to a non-existent object |
fsck flags:
| Flag | What It Does |
|---|---|
--unreachable | Show all unreachable objects |
--dangling | Show only dangling objects (default) |
--lost-found | Write dangling objects to .git/lost-found/ |
--no-dangling | Suppress dangling messages |
--full | Check objects in packfiles too |
Real-Life Analogy
git fsck is like a warehouse audit — checking every box on every shelf against the inventory list. Missing boxes are flagged. Boxes not on any list (dangling) are set aside in the "lost and found."
Visual Architecture
Why It Matters
- Integrity verification: Ensures no objects are corrupt or missing.
- Recovery: Finds dangling commits from deleted branches, cleared stashes.
- Last resort: When reflog is empty,
fsckis the final recovery tool. - Safety: Safe to run anytime — it only reads, never modifies.
Code
# ─── Basic integrity check ───
git fsck
# Checking object directories: 100% done.
# dangling commit abc123...
# ─── Find all unreachable objects ───
git fsck --unreachable
# unreachable commit abc123
# unreachable blob def456
# ─── Save dangling objects to lost-found ───
git fsck --lost-found
# Writes to .git/lost-found/commit/ and .git/lost-found/other/
# ─── Inspect a dangling commit ───
git show abc123
# See if it's the lost commit you're looking for
# ─── Recovery: find lost stashes ───
git fsck --unreachable | grep commit | \
awk '{print $3}' | \
xargs -I {} git log -1 --format="%H %s" {} | \
grep "WIP on"Key Takeaways
git fsckverifies object integrity and reports dangling/missing objects.- Safe to run anytime — it only reads, never writes.
- Dangling commits may be recoverable lost work (deleted branches, stashes).
- Use
--lost-foundto extract dangling objects for inspection.
Interview Prep
-
Q: What is
git fsckand when would you use it? A:git fsckverifies the integrity of all Git objects and references. Use it to check for corruption, find dangling objects, or recover lost commits and stashes when reflog is empty. -
Q: What is a dangling commit? A: A commit that exists in the object database but is not reachable from any branch, tag, or reflog. Common causes: deleted branches, cleared stashes, or abandoned rebase work.
-
Q: How can
git fsckhelp recover lost data? A: Rungit fsck --unreachable | grep committo find orphaned commits. Inspect each withgit show <sha>to identify your lost work. Create a branch pointing to it withgit branch recovery <sha>.