November 26, 2026

Git Reflog: Recover Lost Commits After a Bad Reset or Deleted Branch

Laptop on a wooden desk at night showing a dark terminal window lit by a single desk lamp

HEAD is now at 843fd36 commit 2. That one line is what Git prints after git reset --hard HEAD~2, and it is polite about the fact that two commits just vanished from git log. They are not gone. Git keeps a private journal of every place a branch tip has pointed, and for a few weeks that journal can put the commits back exactly where they were. Everything below was run on an Ubuntu 24.04 test container with Git 2.43.0, in throwaway repositories, so the output is real and the hashes belong to those repositories alone.

Video: Boot dev on YouTube

What the reflog records

A logbook for pointers, kept on one machine only

The git-reflog manual puts it in one line: reference logs “record when the tips of branches and other references were updated in the local repository.” Every commit, reset, checkout, rebase and merge that moves HEAD or a branch leaves an entry. Commits are the cargo; the reflog is the dispatch sheet saying which truck was parked where.

The syntax reads like a stack. The same manual says HEAD@{2} means “where HEAD used to be two moves ago,” so HEAD@{0} is now, HEAD@{1} is the previous position, and so on back. Branches carry their own reflogs too, so main@{1} is where main pointed before its last move.

On a busy repository the list runs to hundreds of lines, and scrolling it by eye is how the wrong hash gets picked. The manual says git reflog show “is an alias for git log -g --abbrev-commit --pretty=oneline,” so the usual git log filters work on it as well. Searching by commit message found a lost commit in one line on the test container:

$ git log -g --grep="feature work" --oneline
45927ed HEAD@{5}: commit: feature work

The same page notes that the HEAD reflog “records branch switching,” which is why it ends up holding work that no branch remembers anymore. --since and --author filter it the same way they filter ordinary history.

That word “local” in the manual is load-bearing. The reflog is not part of what gets pushed or cloned. On the test container, a fresh clone of the practice repository had exactly one entry:

$ git clone -q reflogdemo clone && cd clone && git reflog
2a1800b HEAD@{0}: clone: from /tmp/gt/reflogdemo

Whatever happened in the original repository stays in the original repository. A laptop that dies takes its reflog with it.

Open handwritten logbook on a steel workbench with a brass key resting on the page

Undo a bad git reset –hard

Four commits, two thrown away, two brought back

Set up a repository with four commits, then throw away the top two:

$ git log --oneline
2a1800b commit 4
f1a9c05 commit 3
843fd36 commit 2
65d91df commit 1

$ git reset --hard HEAD~2
HEAD is now at 843fd36 commit 2

$ git log --oneline
843fd36 commit 2
65d91df commit 1

git log walks parent links backward from the current tip, so commits 3 and 4 no longer appear. They still sit in the object database. The reflog shows the trail:

$ git reflog
843fd36 HEAD@{0}: reset: moving to HEAD~2
2a1800b HEAD@{1}: commit: commit 4
f1a9c05 HEAD@{2}: commit: commit 3
843fd36 HEAD@{3}: commit: commit 2
65d91df HEAD@{4}: commit (initial): commit 1

Read it top down. The newest entry is the reset itself, and the line under it is where HEAD stood a moment before the damage: 2a1800b, commit 4. Resetting to that entry undoes the reset:

$ git reset --hard HEAD@{1}
HEAD is now at 2a1800b commit 4

$ git log --oneline
2a1800b commit 4
f1a9c05 commit 3
843fd36 commit 2
65d91df commit 1

Two habits make this safer. First, copy the hash (2a1800b) rather than trusting HEAD@{1}, because every further command adds a new entry and the numbers slide down. Second, when unsure, park the old tip on a new branch instead of resetting: git branch rescue 2a1800b changes nothing in the working tree and can be inspected at leisure.

There is also a shortcut for the most recent mistake. Git sets ORIG_HEAD before a reset moves HEAD, and on the container it pointed straight at the lost tip:

$ git reset -q --hard HEAD~1
$ git log --oneline -1 ORIG_HEAD
2a1800b commit 4
$ git reset -q --hard ORIG_HEAD
$ git log --oneline -1
2a1800b commit 4

ORIG_HEAD holds one value and gets overwritten by the next reset, rebase or merge. The git-rebase manual warns that ORIG_HEAD “is not guaranteed to still point to that commit” if other commands change it mid-rebase, and points to the branch reflog (@{1}) as the dependable route.

Bring back a deleted branch

The branch’s own log dies with it, but HEAD remembers the visit

Deleting a branch with -D is the other classic way to orphan work. The git-branch manual says -D is a “Shortcut for --delete --force,” which skips the check that the branch was merged, and adds: “If the branch currently has a reflog then the reflog will also be deleted.”

So the branch’s diary is shredded along with the branch. git reflog exists, which per the manual “exits with zero status if the reflog exists, and non-zero status if it does not,” returned 1 for the deleted branch and 0 for main. The direct attempt to read it fails outright:

$ git switch -qc feature && echo feat >> f.txt && git add f.txt && git commit -qm "feature work"
$ git switch -q main
$ git branch -D feature
Deleted branch feature (was 45927ed).

$ git reflog show feature
fatal: ambiguous argument 'feature': unknown revision or path not in the working tree.

The rescue comes from a different diary. HEAD was on that branch when the commit was made, and the HEAD reflog is not deleted with any branch:

$ git reflog | head -3
2a1800b HEAD@{0}: checkout: moving from feature to main
45927ed HEAD@{1}: commit: feature work
2a1800b HEAD@{2}: checkout: moving from main to feature

$ git branch feature-restored 45927ed
$ git log --oneline feature-restored | head -2
45927ed feature work
2a1800b commit 4

Git also printed the tip hash in the deletion message itself, (was 45927ed). That parenthesis is the cheapest recovery tool in the whole program, and it scrolls off the terminal within seconds.

Cardboard lost-and-found box of unlabeled items on a basement concrete floor under a bare bulb

When the reflog has nothing to give

Committed work comes back easily, staged work takes a scavenger hunt, and unstaged work was never stored at all

The reflog records pointer moves. If work never became a commit, no pointer ever pointed at it, and the reflog has no entry. Three cases ran on the container, with three different outcomes.

Edits that were never staged. A tracked file was edited, nothing was added, and git reset --hard ran:

$ echo "edited but never staged" >> a.txt; echo untracked > u.txt
$ git reset --hard
HEAD is now at 6d20ccd base
$ cat a.txt
a
$ ls
a.txt  u.txt
$ git fsck --lost-found 2>&1 | grep -c dangling
0

The edit is gone, and no Git command brings it back, because Git never stored it. The untracked file u.txt survived only because reset --hard leaves untracked files alone.

Edits that were staged but never committed. Running git add writes the file’s content into the object database as a blob. After a reset, that blob is dangling, which the git-fsck manual defines as an object “present in the database but never directly used.” Its --lost-found option writes dangling objects into .git/lost-found/, and for blobs “the contents are written into the file”:

$ echo "unsaved draft" > draft.txt; git add draft.txt; git reset -q --hard
$ git fsck --lost-found
dangling blob 12e1302554266328f2320bfb7194b79d4a6cf987
$ cat .git/lost-found/other/*
unsaved draft

The content came back. The filename did not; blobs carry no names, so a real recovery means opening each file and guessing what it used to be called.

A dropped stash. A stash is stored as commits, and git stash drop removes the reference. git fsck --unreachable --no-reflogs lists objects nothing points to, and the stash commit was among them:

$ git stash drop
Dropped refs/stash@{0} (3f70c8355cbcbc7b1fd4e282084b63d9b9919cdc)
$ git fsck --unreachable --no-reflogs 2>/dev/null | grep commit
unreachable commit 5371743df9cef8d63d67a8308f1346a131c0b70f
unreachable commit 3f70c8355cbcbc7b1fd4e282084b63d9b9919cdc
$ git log --oneline -1 3f70c83
3f70c83 WIP on main: 6d20ccd base

Once found, git stash apply 3f70c83 or git branch on that hash restores it.

Where people get burned

The safety net has a timer, and some commands cut it on purpose

The clock. Reflog entries expire. Per the git-gc manual, gc.reflogExpire “defaults to 90 days,” while gc.reflogExpireUnreachable, which covers entries no longer reachable from the current tip, “defaults to 30 days.” A commit discarded by a hard reset is exactly the unreachable kind. Thirty days is the working number, not ninety.

The garbage collector. Once the reflog entry is gone, the commit becomes fair game. The same manual says git gc keeps objects referenced by “reflogs (which may reference commits in branches that were later amended or rewound),” and otherwise prunes loose objects older than a two-week grace period by default. The container showed how quickly the floor drops when someone forces it:

$ git commit -qam second && H=$(git rev-parse --short HEAD); git reset -q --hard HEAD~1
$ git cat-file -t $H
commit
$ git reflog expire --expire=now --all && git gc -q --prune=now
$ git cat-file -t $H
fatal: Not a valid object name 36d3caf

That command pair turns up in advice about shrinking a repository or scrubbing a leaked secret. It works as described. It also deletes every recovery option at once, with no confirmation prompt.

The wrong machine. Because the reflog is local, a CI runner, a fresh clone, or a coworker’s checkout cannot help recover what happened on a laptop. The commits survive only where they were made, unless they were pushed.

Sliding numbers. HEAD@{1} means “one move ago” at the moment of typing. Run a checkout while investigating and the target moves. Hashes stay put; positions do not.

The deleted branch’s reflog. As shown above, git branch -D deletes that branch’s own reflog. People run git reflog show deleted-branch, get a fatal error, and conclude the work is lost when the HEAD reflog still has it.

Office paper shredder with a full bin of shredded strips spilling onto gray carpet

The reflog is not a backup

Recovery from one disk, by one person, within a month

The strongest case against relying on any of this is that the reflog only protects against mistakes made with Git, on the same machine, recently. A failed SSD, a deleted project folder, or rm -rf .git takes the journal and the objects together. It is the same lesson as RAID not being a backup: redundancy inside one box guards against one kind of failure and none of the others.

The practical habit costs nothing. Before any risky operation, drop a marker:

git branch backup/before-rebase

A branch never expires the way a reflog entry does, and it can be pushed somewhere else. The rebase vs merge comparison covers the operation where this habit matters most, because a rebase rewrites every commit it touches and leaves the old versions reachable only through the reflog. When the damage was a bug rather than a lost commit, git bisect is the tool for finding where it came in.

One detail from the tests keeps nagging. The never-staged edit left no dangling blob and no reflog entry, nothing for fsck to count. Git can rewind almost anything it was told about, and nobody told it. How many minutes of work sit unstaged in a working tree right now, protected by nothing but the hope that nobody types reset --hard?

Sources

  • git-reflog documentation: https://git-scm.com/docs/git-reflog
  • git-gc documentation: https://git-scm.com/docs/git-gc
  • git-branch documentation: https://git-scm.com/docs/git-branch
  • git-fsck documentation: https://git-scm.com/docs/git-fsck
  • git-rebase documentation: https://git-scm.com/docs/git-rebase

491 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *