Skip to main content
Git

git reflog: recover lost commits after a bad reset, rebase, or deleted branch

Ran git reset --hard on the wrong commit, botched a rebase, or force-deleted a branch? The commits are almost certainly still there. Here's how to find them with git reflog, and the few cases where they really are gone.

Thien
By Thien
Updated November 21, 2026 · 9 min read
Cassette tape and pencil below letter tiles spelling REWIND on a grey background

You typed git reset --hard HEAD~3 meaning HEAD~1. Or the rebase "finished" and two commits are missing. Or you ran git branch -D on the branch you actually needed. The log says the work is gone, and your stomach agrees.

It almost certainly isn't gone. Git doesn't delete commits when you move away from them. It just stops pointing at them, and it keeps a private diary of every place your branches and HEAD used to point. That diary is the reflog.

Short answer: run git reflog, find the line from just before the mistake, and reset or branch to that hash: git reset --hard <hash> to rewind the current branch, or git branch rescue <hash> to recover it without touching anything else. Committed work stays recoverable for at least 30 days by default. Uncommitted changes that were never staged are the one thing the reflog can't bring back.

What the reflog actually records

Every time HEAD or a branch tip moves (commit, checkout, reset, rebase, merge, pull, commit --amend, even git stash), Git appends a line to a log under .git/logs/. Here's a real one from a throwaway repo where I committed on a feature branch, switched back to main, reset two commits away, and then deleted feature:

$ git reflog
4f3d634 HEAD@{0}: reset: moving to HEAD~2
cd2a8e3 HEAD@{1}: checkout: moving from feature to main
8d6d329 HEAD@{2}: commit: add parser
cd2a8e3 HEAD@{3}: checkout: moving from main to feature
cd2a8e3 HEAD@{4}: commit: step 3
2e9bc5b HEAD@{5}: commit: step 2
4f3d634 HEAD@{6}: commit (initial): step 1

Read it top to bottom as "most recent first." HEAD@{0} is where you are now, HEAD@{1} is where HEAD was one move ago, and so on. Notice two things. First, the add parser commit is still listed even though the branch that held it no longer exists. Second, step 2 and step 3 are listed even though main no longer reaches them. Both are one command away from being back.

These HEAD@{n} names work anywhere Git expects a commit: git show, git diff, git log, git reset, git branch. There are a few more forms worth knowing:

SyntaxMeans
HEAD@{2}Where HEAD was two moves ago
main@{1}Where the main branch tip was before its last move (each branch has its own reflog)
main@{yesterday}, HEAD@{2.hours.ago}Where it pointed at that time
@{-1}The branch you had checked out before this one (what git checkout - uses)
ORIG_HEADWhere HEAD was before the last reset, rebase or merge

Add --date=iso (git reflog --date=iso) when the question is "what did I have at 4pm" rather than "what did I have three moves ago."

Recovery recipes, by disaster

Before any of these: if you're not sure which line is the right one, create a branch first and investigate second. git branch rescue <hash> costs nothing, changes nothing in your working tree, and makes the commit reachable again so nothing can garbage-collect it while you think.

"I reset --hard to the wrong commit"

The reflog line right below the reset: entry is where you were.

git reflog                     # find the line before "reset: moving to ..."
git reset --hard HEAD@{1}      # or the hash from that line

If the reset was the very last thing you did, git reset --hard ORIG_HEAD does the same thing without reading the log. ORIG_HEAD is overwritten by the next reset, rebase or merge though, so the reflog is the reliable route once you've done anything else.

"My rebase ate commits" (or I just hate the result)

A rebase writes several reflog entries for HEAD: a rebase (start), one rebase (pick) per replayed commit, and a rebase (finish). From the same demo repo after rebasing topic onto main:

87c6ff7 HEAD@{0}: rebase (finish): returning to refs/heads/topic
87c6ff7 HEAD@{1}: rebase (pick): topic work
3422f53 HEAD@{2}: rebase (start): checkout main
e0cb557 HEAD@{3}: checkout: moving from main to topic

The pre-rebase state is the line just below rebase (start), here e0cb557. Counting HEAD entries through a long interactive rebase gets error-prone, so use the branch's own reflog instead. A rebase moves the branch tip exactly once, so topic@{1} is the branch as it was before:

git reset --hard topic@{1}

ORIG_HEAD also works immediately after the rebase. If you were mid-squash in an interactive rebase and it hasn't finished, git rebase --abort is simpler still.

"I deleted a branch with -D"

git branch -D deletes the branch's own reflog along with the branch. Try git reflog show feature afterwards and Git answers with fatal: ambiguous argument 'feature': unknown revision. But HEAD's reflog still remembers every commit you made while that branch was checked out:

git reflog | grep -m1 'feature'        # or look for its last commit message
git branch feature 8d6d329              # recreate it at that commit

Helpfully, git branch -D prints the tip it deleted (Deleted branch feature (was 8d6d329).), so if your terminal scrollback survives, that hash is the fastest fix of all.

The one case this doesn't cover: a branch you fetched but never checked out has never moved your HEAD, so it never landed in HEAD's reflog. If it still exists on the remote, git fetch brings it back. If you're the only one who had it, skip to the git fsck section below.

"I amended a commit and lost the old version"

git commit --amend creates a new commit and moves the branch to it. The original is HEAD@{1}:

git diff HEAD@{1} HEAD          # what did the amend change?
git reset --soft HEAD@{1}       # undo the amend, keep the changes staged

"I force-pushed over a teammate's work"

Your reflog only knows about moves in your clone. Their commits are in their reflog, or, if you'd fetched them before the force-push, in your remote-tracking branch's reflog: git reflog show origin/main. Find the line before your push and git push --force-with-lease origin <hash>:main to put it back. Then switch your team to --force-with-lease permanently, which refuses to overwrite commits you haven't seen.

The reflog is local and private. It isn't pushed, it isn't cloned, and a fresh clone starts with an empty one. "Recover it from the reflog" only works on the machine where the mistake happened, which is also the main reason not to rm -rf a broken clone and re-clone in a panic.

When the reflog isn't enough: git fsck

Some commits were never pointed at by HEAD or a branch. The classic is a dropped stash. A stash is a commit referenced only by refs/stash, and git stash drop or git stash clear removes that reference. The commit itself lingers until garbage collection. git fsck finds objects that nothing references:

$ git stash drop
$ git fsck --no-reflogs --unreachable | grep commit
unreachable commit 8d9c171b792e66f1ab75dc0b117019dead069c7e
unreachable commit f879d78262ef36e1d7848c8dfda2b4e0fe185e3e

A stash is stored as two or three commits (working tree, index and, with -u, untracked files), which is why one stash gives you two hits. The git stash docs give a one-liner that filters to stash-shaped merge commits:

git fsck --unreachable | grep commit | cut -d' ' -f3 \
  | xargs git log --merges --no-walk --grep=WIP

Once you've spotted the right one, git stash apply <hash> brings it back. The same trick recovers branches that never touched your HEAD, as long as the objects exist locally.

What's actually unrecoverable

Being honest about the limits matters more than the recipes:

  1. Changes you never staged. git reset --hard and git checkout -- file overwrite working-tree files that Git never stored. No commit, no blob, nothing to find. Your editor's local history (VS Code's Timeline, JetBrains' Local History) is the last hope here.
  2. Changes you staged but never committed are mostly recoverable, just painfully. git add writes a blob, so after a reset --hard, git fsck --lost-found writes the dangling blobs to .git/lost-found/other/. You get the file contents back, without filenames. In my test the staged notes.txt came back intact as .git/lost-found/other/d1cb8a1…. Fine for one file, miserable for forty.
  3. Anything older than the expiry window. Reflog entries for commits still reachable from a branch expire after 90 days (gc.reflogExpire). Entries for commits that are not reachable, which are exactly the ones you're trying to rescue, expire after 30 days (gc.reflogExpireUnreachable). After that, git gc can prune the objects for real. Note that git gc --auto runs quietly after ordinary commands like commit and merge, so you can't count on "I haven't run gc."
  4. Anything after someone ran git reflog expire --expire=now --all && git gc --prune=now. That's the documented way to purge secrets from a repo, and it does exactly what it says.

If a repository holds work you can't afford to lose, raise those limits: git config gc.reflogExpireUnreachable 90.days costs a little disk space and buys you two more months of undo.

FAQ

Is git reflog the same as git log -g?

Yes. git reflog is shorthand for git log -g --abbrev-commit --pretty=oneline. Use the long form when you want real log formatting on reflog entries, for example git log -g --format='%h %gd %gs (%cr)' to add relative times.

Does git reflog work on a bare repository?

Not by default. core.logAllRefUpdates is off for bare repos, which is why a server-side force-push can't be undone from the server's reflog unless someone turned it on. Most hosting platforms keep their own push history instead. GitHub, for instance, shows force-pushes in a pull request's timeline.

Can I recover a commit that was never pushed from another machine?

Only from that machine. Neither commits nor the reflog leave a clone until you push.

The habit that makes this boring

The reflog turns "I destroyed my work" into a 30-second detour, but only for work that was committed. That's the real lesson: commit early and often on local branches, even messy WIP commits, because a commit is a recovery point and a dirty working tree isn't. You can clean the history up afterwards with interactive rebase, stash the half-finished bits, and bisect your way through it later. If you'd rather not remember all the @{} syntax, our Git Cheatsheet has the reflog and reset commands next to everything else.

Cover photo by DS stories on Pexels.

References

Primary documentation and specifications checked when this article was last updated.

GitDeveloper ToolsWorkflow

Related articles

All articles