git reflog: Recovering Work You Think You Destroyed

git reflog: Recovering Work You Think You Destroyed

The sinking feeling after git reset --hard on the wrong branch is almost always unjustified. Git does not delete commits when you stop pointing at them, and the reflog is the record of every position your references have held.

What it is

git reflog
a1b2c3d (HEAD -> main) HEAD@{0}: commit: add feature
e4f5g6h HEAD@{1}: reset: moving to HEAD~2
i7j8k9l HEAD@{2}: commit: work in progress
m0n1o2p HEAD@{3}: checkout: moving from feature to main
q3r4s5t HEAD@{4}: rebase finished: returning to refs/heads/main

Every entry is a position HEAD has held and the operation that moved it. Read it bottom-up to follow what you did.

# with dates, which is how you find "before lunch"
git reflog --date=iso
git reflog --date=relative

# for a specific branch, not just HEAD
git reflog show main
git reflog show feature-x

# every reference
git reflog --all

git reflog show <branch> is underused. git reflog alone shows HEAD’s history, which is not the same as where a particular branch has pointed.

The recoveries

After a hard reset

# the mistake
git reset --hard HEAD~3
# three commits of work apparently gone

# find where you were
git reflog
# e4f5g6h HEAD@{1}: commit: the work you wanted

# go back
git reset --hard HEAD@{1}
# or by hash, which is less ambiguous
git reset --hard e4f5g6h

Prefer the hash over HEAD@{n}. The reflog shifts as you run commands, so HEAD@{1} means something different after your next operation. Copy the hash.

A deleted branch

git branch -D feature-x
# Deleted branch feature-x (was a1b2c3d).

Git tells you the hash in the deletion message, which is the first place to look.

# recreate it
git branch feature-x a1b2c3d

# if you did not note the hash
git reflog | grep -i 'feature-x'
git reflog --all | grep -i 'feature-x'
# or find it by content
git log --all --oneline --source | grep -i 'the commit message'

A rebase that went wrong

# before: git rebase -i HEAD~5, and something is now wrong

git reflog
# a1b2c3d HEAD@{0}: rebase (finish): returning to refs/heads/main
# ...
# z9y8x7w HEAD@{7}: rebase (start): checkout main~5

# the commit before the rebase started
git reset --hard ORIG_HEAD

ORIG_HEAD is set before merge, rebase and reset, which makes it the fastest undo for exactly these operations.

git reset --hard ORIG_HEAD
git merge --abort          # during a merge
git rebase --abort         # during a rebase

--abort works while the operation is in progress. ORIG_HEAD works after it has finished, and it holds only the most recent one. Our git branching guide covers the operations themselves.

Commits you made on a detached HEAD

git checkout a1b2c3d       # detached
# ... commit several times ...
git checkout main          # "You are leaving 3 commits behind"

Git warns, and the warning scrolls past.

git reflog
# find the last commit you made while detached
git branch rescue-work <that-hash>
git checkout rescue-work

A dropped stash

git stash list             # empty, and you are sure there was one
git fsck --unreachable | grep commit
# stashes are commits, so look for the shape
git fsck --unreachable | awk '/commit/ {print $3}' | while read -r h; do
    git log -1 --format='%h %s' "$h"
done | grep -i 'WIP on'

A stash is a commit with a message starting WIP on, so this finds it.

git stash apply <hash>
# or
git checkout -b recovered <hash>

Our git basics guide covers stashing, and the recurring advice applies: a worktree is usually better than a stash, because there is nothing to drop.

git fsck, when the reflog does not have it

# every object nothing points at
git fsck --unreachable
git fsck --lost-found

# the dangling ones specifically
git fsck --dangling
unreachable commit a1b2c3d4...
dangling blob e4f5g6h7...
dangling commit i7j8k9l0...
# inspect before recovering
git show a1b2c3d
git log -1 --stat a1b2c3d

# recover
git branch recovered a1b2c3d

--lost-found writes the objects into .git/lost-found/ as files, which is useful for a dangling blob: a file that was added to the index and then lost before being committed.

git fsck --lost-found
ls .git/lost-found/other/
# inspect them, they are file contents
for f in .git/lost-found/other/*; do
    echo "=== $f"; head -5 "$f"
done

That is the recovery path for git add followed by something that blew away the index, which the reflog does not cover because no reference ever pointed at it.

What is genuinely unrecoverable

Being clear, because the reflog is not magic.

Changes never staged or committed. If it was only in the working tree and you ran git checkout -- file or git restore file, it is gone. Git never saw it.

# this discards uncommitted changes permanently
git checkout -- file
git restore file
git reset --hard        # for tracked files with uncommitted changes

Untracked files removed by git clean.

# always dry-run first
git clean -nd
# then
git clean -fd

Anything after garbage collection has expired it. Unreachable objects go after 30 days by default, reachable reflog entries after 90.

# the settings
git config gc.reflogExpire                    # 90 days
git config gc.reflogExpireUnreachable         # 30 days
git config gc.pruneExpire                     # 2 weeks

# and the thing never to run while recovering
git gc --prune=now --aggressive
git reflog expire --expire=now --all

Do not run git gc --prune=now when you are trying to recover something. It does exactly what you do not want.

A repository that only ever existed on a lost machine. The reflog is local and is not pushed, so a fresh clone has none of it.

Making recovery easier beforehand

# push work-in-progress branches, so the remote has a copy
git push -u origin feature-x

# or just a ref, without a branch
git push origin HEAD:refs/wip/colton/feature-x
# commit more often, amend later
git commit -m 'wip'
# ... and later
git rebase -i origin/main   # squash it into something sensible

A commit is the cheapest insurance in git. Frequent scrappy commits that get squashed before review cost nothing and make everything above unnecessary.

# a pre-rebase safety branch, as a habit
git branch backup-$(date +%F-%H%M)
git rebase -i HEAD~10
# if it goes wrong
git reset --hard backup-2026-11-23-1432

Our date guide covers the timestamp format, which sorts properly so git branch --list 'backup-*' lists them in order.

# and clean them up occasionally
git branch --list 'backup-*' | head -20

Useful reflog queries

# where was main an hour ago
git show 'main@{1 hour ago}'
git diff 'main@{yesterday}' main

# what did HEAD do today
git reflog --date=iso | grep "$(date -I)"

# everything that has touched a file, including through rebases
git log --all --follow --oneline -- path/to/file

# find a commit by its content
git log --all -S 'function_name' --oneline
git log --all -G 'regex' --oneline

git log -S searches for commits that changed the number of occurrences of a string, which is how you find where a function was deleted. -G takes a regex. Both search the whole history, and with --all they search every reference.

# which branches contain a commit
git branch --all --contains a1b2c3d

That last one answers “did this fix make it into the release branch”, which comes up more often than recovery does.

Frequently Asked Questions

What is the git reflog?

A local log of every position each reference has held, including HEAD. Every commit, checkout, reset, merge and rebase appends an entry. It means a commit you can no longer reach from any branch is still findable by the position HEAD held before you moved it.

Can I recover a commit after a hard reset?

Yes, in nearly every case. The commit still exists in the object database and the reflog records where HEAD was before the reset. Find the entry, then reset back to it or create a branch pointing at it. The exception is after garbage collection has run on unreachable objects, which takes weeks by default.

How long do reflog entries last?

Ninety days for reachable commits and thirty for unreachable ones, by default. Those are configurable through the gc reflogExpire settings. In practice you have weeks rather than days, which is long enough for any recovery you realise you need.

Is the reflog pushed to the remote?

No. It is entirely local and per-repository, so a fresh clone has an empty reflog. That means it cannot help with work that only ever existed on a machine you no longer have, and it also means your experiments are not visible to anyone else.

What is ORIG_HEAD and when is it set?

A reference to where HEAD was before a potentially dangerous operation such as a merge, rebase or reset. It gives you a one-word way to undo the last such operation without reading the reflog, though it only holds the most recent one.

How do I find a commit that is not in any reflog entry?

Use git fsck with the lost-found or unreachable options, which reports dangling commits and blobs in the object database. That covers cases where the reflog entry has expired or the work was committed in a state the reflog did not capture, such as a stash that was dropped.