6. Recovering the Lost

Reflog spelunking, fsck --lost-found, and why your "lost" commit is almost never actually lost

Everything in the previous five articles converges here. The object store is immutable. Refs are cheap pointers. The reflog records every movement. Put those three facts together and the following becomes true: if you "lost" a commit in the last 90 days, it is almost certainly still sitting in .git/objects/, and we can find it.

This article is a field manual. Six scenarios, six recoveries. Each one walks from "I did a thing I regret" to "it's back."

Scenario 1: "I hard-reset and lost three commits"

You ran git reset --hard HEAD~3 to try something, and now your last three commits are gone from git log.

They're not gone. They're orphaned. Find them with the reflog:

$ git reflog 7a9f31e HEAD@{0}: reset: moving to HEAD~3 3e8f4c7 HEAD@{1}: commit: add password reset flow ← the tip we want back 1b2c3d4 HEAD@{2}: commit: validate email format af5626b HEAD@{3}: commit: wire up signup form c3b8f4e HEAD@{4}: commit: scaffold auth routes …

$ git reset --hard 3e8f4c7 HEAD is now at 3e8f4c7 add password reset flow

Or equivalently git reset --hard HEAD@{1}. Either way — branch restored, all three commits back.

Scenario 2: "I rebased and it went wrong"

You did an interactive rebase, resolved conflicts for twenty minutes, and the result is subtly broken. You want the branch back the way it was.

Git sets ORIG_HEAD before any operation that rewrites a lot of commits (rebase, merge, reset). So:

$ git reset --hard ORIG_HEAD

If you've done other things since and ORIG_HEAD no longer points where you want, the reflog has the answer. Look for rebase (start) and the line just before it — that's your pre-rebase tip:

$ git reflog d4e5f6a HEAD@{0}: rebase (finish): returning to refs/heads/feature 9b2c1f4 HEAD@{1}: rebase (pick): apply email validation 8a3c7d2 HEAD@{2}: rebase (pick): scaffold signup e1f2a3b HEAD@{3}: rebase (start): checkout main 3e8f4c7 HEAD@{4}: commit: WIP before the rebase ← this one …

$ git reset --hard HEAD@{4}

Scenario 3: "I deleted a branch that had unmerged commits"

You ran git branch -D feature-x. The commits on it weren't merged anywhere. git branch no longer lists it and git log can't see those commits.

When you delete a branch, Git prints the final commit hash as a sort of receipt:

Deleted branch feature-x (was 3e8f4c7).

If you saw that in your terminal, you're done — git branch feature-x 3e8f4c7 and you have it back. If you didn't, the deleted branch's own reflog persists for a bit in .git/logs/refs/heads/feature-x even after the ref file is gone. On more recent Git versions, git reflog won't show it but you can cat the log file directly, or use:

$ git fsck --lost-found Checking object directories: 100% (256/256), done. dangling commit 3e8f4c7a912b5d6e0f1a2b3c4d5e6f7a8b9c0d1e dangling commit 9b2c1f47e0d2a3b4c5d6e7f8a9b0c1d2e3f4a5b6 dangling blob af5626b4a114abcb82d63db7c8082c3c4756e51b

"Dangling" means: reachable from nothing. That's every orphan still in the object store. For each dangling commit, git show <hash> to see what it is, and git branch recovered <hash> to resurrect it as a branch.

Scenario 4: "I force-pushed over my own branch"

You did git push --force, and then realized the version you pushed wasn't the one you wanted.

The remote won't help you here — it has no reflog by default (some hosts do, like GitHub's "Activity" / git ls-remote can show previous tip hashes in some cases). But your local reflog still records the old tip:

$ git reflog feature 9b2c1f4 feature@{0}: reset: moving to 9b2c1f4 ← what you force-pushed 3e8f4c7 feature@{1}: commit: the version you wanted

$ git reset --hard 3e8f4c7 $ git push --force-with-lease

And the lesson: --force-with-lease over --force. It refuses to push if the remote has commits you haven't seen, which catches exactly this class of mistake.

Scenario 5: "I committed to detached HEAD and then checked out a branch"

You checked out a tag to test something, made a few commits while in detached HEAD, then ran git checkout main. Your commits are gone from git log and no branch points at them.

HEAD's reflog captured every commit you made while detached:

$ git reflog 8a3c7d2 HEAD@{0}: checkout: moving from detached to main 9b2c1f4 HEAD@{1}: commit: experimental fix 3e8f4c7 HEAD@{2}: commit: try a different approach 1b2c3d4 HEAD@{3}: checkout: moving from main to v1.0 (detached)

$ git branch experimental 9b2c1f4

The branch experimental now points at the tip of your detached-HEAD work. The two new commits are reachable again.

Scenario 6: "I lost a single file I had staged but never committed"

You ran git add important.txt, then something clobbered it — maybe git checkout ., or an editor save-over-restore. The file is gone from disk and was never in a commit.

This one's subtle. A staged file is written to the object store as a loose blob, even though no commit references it. It's dangling:

$ git fsck --lost-found dangling blob af5626b4a114abcb82d63db7c8082c3c4756e51b dangling blob 1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c dangling blob c3b8f4e1d2c3b4a5e6f7a8b9c0d1e2f3a4b5c6d7

$ git cat-file -p af5626b4 | head # the first lines of each candidate blob, to identify yours

$ git cat-file -p af5626b4 > important.txt

git fsck --lost-found even drops a convenience file at .git/lost-found/other/<hash> for each dangling blob, so you can grep their contents directly instead of cat-ing by hash.

The recovery toolbox, in one place

Commands worth memorizing

CommandUse
git reflogEvery HEAD movement. First stop for any "undo".
git reflog show <ref>Per-branch reflog — shows where that specific branch has been.
git reset --hard ORIG_HEADUndo the last rebase/reset/merge.
git fsck --lost-foundList every dangling object. Use when the reflog isn't enough (branch deletion, dropped staging).
git cat-file -p <hash>Pretty-print any object. Works on dangling objects too.
git show <hash>Summary of a commit or blob — identifies dangling commits quickly.
git branch <name> <hash>Resurrect a commit as a named branch. The act of creating a ref keeps it from GC.
git stash listDon't forget — work you stashed might also be what you're looking for.

The 90-day deadline (and how to extend it)

Everything in this article assumes the objects haven't been garbage-collected yet. The defaults are:

  • Reflog entries expire after 90 days (gc.reflogExpire).
  • Unreachable reflog entries expire after 30 days (gc.reflogExpireUnreachable).
  • Loose unreachable objects older than 2 weeks are pruned by gc (gc.pruneExpire).

If you're worried you might need something later, nothing stops you from being conservative:

$ git config gc.reflogExpire "1 year" $ git config gc.reflogExpireUnreachable "1 year"

For an actively debugged repo on your own machine, the disk cost is trivial. If something really matters and you want to be sure it survives any GC, just tag it:

$ git tag keep/recovery 3e8f4c7

Any tag keeps its target — and the target's entire ancestry — permanently reachable.

When recovery actually fails

There are three ways to genuinely lose work in Git:

  1. The data never existed in Git. Files you didn't git add aren't in the object store. No amount of reflog spelunking brings them back. (This is what .gitignore'd files look like, and what "I thought I'd committed that" feels like.)
  2. You explicitly ran git gc --prune=now. This bypasses the grace periods and deletes unreachable objects immediately. Rare, but people do it trying to shrink repos.
  3. You force-pushed to a shared branch and then cleaned your local clone. The rewritten commits are gone from both sides and eventually GC'd.

For everything else, if you can type git reflog and see it in the list, or find it as a dangling object, it's recoverable.

The one-liner worth tattooing on the inside of your wrist

Before reaching for Stack Overflow, reach for git reflog. Nine times out of ten, the answer is already in it.

Try It Yourself — Recovery Playground

Practice the recovery workflows from this article. Use the "break" buttons to simulate disasters, then use the reflog and recovery tools to get your commits back. Orphaned commits appear as dashed ghosts in the graph — they're still in the object store until you find them.

Break it
Fix it

Commit graph

Reflog

Dangling objects

What to take away from the whole series

  • Objects are immutable and content-addressed. Nothing ever really changes; new things get written and old ones get orphaned.
  • Refs are pointers. Branches, tags, HEAD — all text files with hashes in them.
  • Pack files and delta compression make the immutable object store cheap.
  • The reflog is the universe's backup tape. Every ref movement is recorded. 90 days of grace before anything is gone.
  • "Rewriting history" means building new commits and moving refs. It's always non-destructive locally; only force-push on shared branches hurts.
  • Recovery is almost always possible because almost nothing is truly deleted. You just have to know where to look.

— End of series —