Lesson Completion
Back to course

git fsck

Intermediate
7 minutes4.7Git

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:

StatusMeaning
dangling commitA commit no branch or tag points to
dangling blobA blob not referenced by any tree
dangling treeA tree not referenced by any commit
missing objectAn object referenced but not found
broken linkA reference to a non-existent object

fsck flags:

FlagWhat It Does
--unreachableShow all unreachable objects
--danglingShow only dangling objects (default)
--lost-foundWrite dangling objects to .git/lost-found/
--no-danglingSuppress dangling messages
--fullCheck 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

flowchart TD FSCK["git fsck"] --> CHECK["Verify all objects"] CHECK --> OK["✅ Objects intact"] CHECK --> DANGLING["⚠️ Dangling objects"] CHECK --> CORRUPT["❌ Corruption found"] DANGLING --> RECOVERY["🔧 Potential recovery targets"] style FSCK fill:#0f3460,stroke:#53d8fb,color:#53d8fb style DANGLING fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style CORRUPT fill:#2d1b1b,stroke:#e94560,color:#e94560

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, fsck is the final recovery tool.
  • Safety: Safe to run anytime — it only reads, never modifies.

Code

bash
# ─── 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 fsck verifies 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-found to extract dangling objects for inspection.

Interview Prep

  • Q: What is git fsck and when would you use it? A: git fsck verifies 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 fsck help recover lost data? A: Run git fsck --unreachable | grep commit to find orphaned commits. Inspect each with git show <sha> to identify your lost work. Create a branch pointing to it with git branch recovery <sha>.

Topics Covered

Git InternalsRecovery

Tags

#git#internals#fsck#recovery

Last Updated

2026-02-13