Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 78 ยท Inspecting History

The Reflog and Recovering Lost Work

Use git reflog to recover commits after reset --hard, deleted branches and bad rebases. Learn HEAD@{n}, per-branch reflogs, expiry, fsck and what Git can lose.

Every Git user eventually types a command and then stares at the screen. A git reset --hard that went one commit too far. A branch deleted with -D that still had unmerged work. A rebase that turned a clean history into something unrecognizable. The log no longer shows the commits you need, and it feels as if hours of work disappeared.

In most of these cases the work is not gone. Git keeps a private journal of where your branches and HEAD have pointed, called the reflog (reference log). Commits that no branch reaches any more still sit in the object database for weeks. The reflog is the index card that tells you their hashes. Knowing how to read it turns a panic into a two-line fix, and understanding its limits tells you what Git genuinely cannot recover.

What the reflog is

Normal git log walks the ancestry of a commit: parents of parents. The reflog is different: it is a chronological list of the values a reference held on your machine. Each time HEAD or a branch moves because of a commit, checkout, merge, reset, rebase, pull or amend, Git appends a line to a file under .git/logs/.

TEXT
.git/logs/HEAD                     every move of HEAD
.git/logs/refs/heads/main          every move of the branch main
.git/logs/refs/heads/feature       ...and so on, one file per branch

Three facts to remember:

  1. It is local. The reflog is never pushed, fetched or cloned. A fresh clone has an almost empty reflog. Your colleague's reflog cannot rescue your work, and yours cannot rescue theirs.
  2. It expires. Entries older than 90 days are removed (30 days for entries whose commits are unreachable) when Git runs garbage collection. The settings are gc.reflogExpire and gc.reflogExpireUnreachable.
  3. It records pointer moves, not file contents. If you never committed or staged a change, the reflog knows nothing about it.

Reading the reflog

git reflog is short for git reflog show HEAD. Each line has the hash HEAD had after the action, a shorthand HEAD@{n} (n = how many moves ago), and a description of what happened. The newest entry is HEAD@{0}.

Bash The reflog records every move of HEAD

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Read it from the bottom up to replay your session: the first commit (commit (initial)), the next commit, the branch switches, the fast-forward merge, the amend and finally the reset. Each line starts with the hash that HEAD pointed to after the action. The text before the colon (commit, checkout, merge, reset, commit (amend)) names the action, which is what lets you pick the right moment to go back to.

You may add dates with git reflog --date=iso (or git log -g --date=relative). git log -g (walk reflogs) formats reflog entries like normal log entries, so all the pretty-print options work.

Recovery 1: undoing a hard reset

git reset --hard moves the branch and overwrites your files, but it does not delete commits. Two ways to get back:

  • ORIG_HEAD: before a risky operation (reset, merge, rebase, pull) Git stores the previous HEAD in ORIG_HEAD. So right after a bad reset, git reset --hard ORIG_HEAD undoes it. Note that the next such operation overwrites it.
  • Reflog: find the entry from before the accident, then git reset --hard 'HEAD@{n}' or by hash.
Bash Recover commits after reset --hard

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

All three commits and their files are back, because the commits were never deleted. This only works for committed work: files that were modified but never committed or staged are overwritten by --hard with no copy anywhere.

If you are not sure whether an operation is safe, mark your position first: git branch backup. A branch costs nothing, and it keeps the commits reachable so the reflog is not even needed.

Recovery 2: a deleted branch

git branch -D name removes the label, not the commits. If the branch was not merged, those commits are now unreachable, but still stored. The reflog of HEAD remembers the last time you were on that branch.

Bash Bring back a branch deleted with -D

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

We found the commit by searching the reflog for its message, then created a branch pointing at its hash. In real life you often scan git reflog by eye, or use the extra form git reflog show --all. Note that for a deleted branch, the entry checkout: moving from experiment to main also gives you the branch name; the entry just before it is the tip. (If you have that name, git reflog show experiment no longer works, because the branch's own reflog is deleted with it.)

Per-branch reflogs

Besides the reflog of HEAD, every branch has its own: git reflog show main (or main@{n}) lists only moves of that branch. This is cleaner when you want to see what happened to a specific branch: commits, resets, merges, rebases and force-pushes (as seen by pulls).

The @{...} syntax understands time as well, based on your own machine's records: main@{yesterday}, main@{3.days.ago}, HEAD@{2026-01-05}. See Revision Syntax.

Recovery 3: after a bad rebase or amend

git rebase, git commit --amend and git pull --rebase all create new commits and move the branch to them. The old ones stay in the object database. Right after a rebase, the old tip is ORIG_HEAD, and in the branch reflog it is the entry just before the rebase began.

Bash Undo a rebase using the branch reflog

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The branch reflog records the whole rebase as one entry (rebase (finish)), so topic@{1} is the old tip. The HEAD reflog is busier: it has a line for the start of the rebase and one for every replayed commit, so there you would use ORIG_HEAD, or look for the entry just before rebase (start). The pattern is always the same: find the last line from before the mistake, then reset or branch to that hash.

Dangling commits and git fsck

What if the reflog entry has expired, or you ran git reflog expire, or the commit was never on a branch (a stash that was dropped, a detached HEAD session)? The commit may still exist as a dangling or unreachable object. git fsck (file system check) walks the whole object database and reports objects nothing points to:

  • git fsck --lost-found lists dangling commits and also writes them into .git/lost-found/.
  • git fsck --unreachable --no-reflogs shows everything that is not reachable from any ref or reflog.

Output lines look like dangling commit <hash>. You inspect each with git show <hash> and rescue the ones you want with git branch rescued <hash>. A dropped stash (see Stashing Work in Progress) is a typical find: it appears as a dangling commit whose message begins with WIP on ....

Bash Find a lost commit with git fsck after the reflog is gone

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Even with the reflog wiped, the commit survived because garbage collection had not yet removed it. Run git gc --prune=now after expiring the reflog and the object is truly deleted. Normal git gc is gentler: it keeps unreachable objects for two weeks by default (gc.pruneExpire), and Git rarely runs it by hand, so you usually have a generous window.

What Git can and cannot recover

Situation Recoverable? How
reset --hard of committed work Yes ORIG_HEAD or reflog
Deleted branch Yes reflog, git branch name <hash>
Bad rebase, amend, merge Yes ORIG_HEAD, branch@{n}
Dropped stash Yes, for a while git fsck --unreachable, look for WIP on commits
Commit made on a detached HEAD, then switched away Yes reflog (Git prints a warning with the hash when you leave)
Edits never staged or committed, then restore, checkout ., reset --hard, clean No no copy exists (an editor's own backup may help)
Staged but never committed, then overwritten Often git fsck --lost-found lists the dangling blobs (file contents)
After gc --prune=now or the expiry period No objects deleted
Work that exists only in someone else's clone Not from your reflog ask them

The practical rule: commit early. A commit, even an ugly "WIP", puts your work under the reflog's protection. You can always clean it up later with an interactive rebase. The next lesson on git reset explains exactly what each reset mode does to your files, so you can predict which mistakes are survivable.

The reflog is not a backup. It lives in the same .git folder as everything else: delete the repository or lose the disk and it goes with it. It is also local and time-limited. Push important branches to a remote.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the reflog record?

    The reflog is a local journal of pointer movements. It does not contain unstaged edits and is not shared with other clones.
  2. 2. You ran git reset --hard HEAD~2 by mistake. Right now, what is the quickest way to return to where you were?

    Git saves the previous HEAD in ORIG_HEAD before a reset, so resetting to it restores the earlier state, as long as no other such operation has overwritten it.
  3. 3. After git branch -D feature, how can you get the branch back?

    Deleting a branch only removes the label. The commits remain until garbage collection, and the HEAD reflog still shows their hashes.
  4. 4. Which situation cannot be repaired with the reflog?

    Unstaged, uncommitted changes were never stored in the object database, so there is nothing for the reflog or fsck to find.
  5. 5. The reflog entry for a lost commit has expired. What can you still try?

    Expiring a reflog entry does not delete the commit. Until gc prunes the unreachable object, fsck can still find it.

Summary

  • The reflog is a local, time-limited journal of where HEAD and each branch pointed; git reflog shows it, HEAD@{n} and branch@{n} refer to its entries.
  • git reset --hard ORIG_HEAD undoes the most recent reset, merge, rebase or pull.
  • To recover a deleted branch or reset commits: find the hash in the reflog, then git branch name <hash> or git reset --hard <hash>.
  • git fsck --lost-found finds dangling commits (dropped stashes, expired reflog entries) until gc prunes them (about two weeks by default).
  • Only committed (or at least staged) work is recoverable; uncommitted edits that are overwritten are gone.
  • Commit early and push important branches; the reflog is a safety net, not a backup.

Next: git reset in Depth