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 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:
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 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:
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:
"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 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 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 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
| Command | Use |
|---|---|
git reflog | Every 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_HEAD | Undo the last rebase/reset/merge. |
git fsck --lost-found | List 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 list | Don'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:
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:
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:
- The data never existed in Git. Files you didn't
git addaren'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.) - 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. - 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.
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 —