The reflog and recovering lost work

Intermediate · 14 min

Use the reflog to find commits that no branch points to any more: undo a hard reset, bring back a deleted branch, and learn when Git really does delete them.

Before thisReset, soft, mixed and hardHow Git stores history

System Crafters · 14 min

The reflog and recovering lost work

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by System CraftersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The reflog is a local, private log of where `HEAD` and each branch pointed over time, and it records every checkout, commit, reset and rebase.
  • To recover from a bad `reset --hard` or rebase, find the commit's hash in `git reflog` and move a branch there with `git reset --hard <hash>` or `git branch <name> <hash>`.
  • A deleted branch's commits are not gone: note the hash that `git branch -D` prints, or find it in the reflog, and recreate the branch.
  • The reflog is per clone and never pushed, so it can't rescue work in another clone, and it protects only committed work.
  • Unreachable commits are removed only after the reflog entries expire and `git gc` prunes them, 90 and 30 days by default.

The scenario everyone hits once

You are tidying the jot repository and run git reset --hard HEAD~2. A second later you realize the commits you just discarded included the export feature. The branch no longer points at them, and git log doesn’t show them. It feels like they were deleted.

They weren’t. A commit is an object in .git/objects, and objects aren’t removed when a branch stops pointing at them. They become unreachable, and Git collects them later. In the meantime, Git keeps a diary of where your pointers have been: the reflog.

Reading the reflog

I built a repository with three notes on main, plus a branch export with two more commits, then went back to main and made the mistake:

$ git reset --hard HEAD~2
HEAD is now at f2ac225 Add note 1
$ git reflog
f2ac225 HEAD@{0}: reset: moving to HEAD~2
53b3e45 HEAD@{1}: checkout: moving from export to main
304ef4f HEAD@{2}: commit: Export as JSON
08485c5 HEAD@{3}: commit: Add export script
53b3e45 HEAD@{4}: checkout: moving from main to export
53b3e45 HEAD@{5}: commit: Add note 3
efb3aa9 HEAD@{6}: commit: Add note 2
f2ac225 HEAD@{7}: commit (initial): Add note 1

(Hashes will differ for you.) Each line says where HEAD was and what moved it. HEAD@{1} is “where HEAD was one move ago”, and it holds the commit from before the reset. Recovery is a normal reset to that point:

$ git reset --hard 'HEAD@{1}'
HEAD is now at 53b3e45 Add note 3

Quote HEAD@{1} so your shell doesn’t treat the braces as syntax. The reflog is also searchable by time, as in main@{yesterday}, and each branch has its own log: git reflog show main.

Bringing back a deleted branch

The export commits were still on the export branch, so the reset never touched them. I deleted that branch too:

$ git branch -D export
Deleted branch export (was 304ef4f).

Git told me the hash. If you’d missed it, the commit is still in the HEAD reflog (here as HEAD@{3}, “commit: Export as JSON”). Recreate the branch from it:

$ git branch export 304ef4f
$ git log --oneline export
304ef4f Export as JSON
08485c5 Add export script
53b3e45 Add note 3
...

Pointing a new branch at a lost commit is usually better than resetting your current branch onto it, since you can inspect before deciding.

What the reflog can’t do

  • Uncommitted work. If you reset --hard away edits you never committed or staged, no object was ever written. It is simply gone. Committing early, even a throwaway “wip”, is what puts work under the reflog’s protection.
  • Other clones. The reflog lives in .git/logs/ of this clone only, and is never pushed or fetched.
  • Forever. Entries expire.

When Git really deletes

Reflog entries expire (90 days for reachable ones, 30 for unreachable, by default), and then git gc can prune the objects. I forced the process on a small repository to show each stage. After deleting a branch, ordinary git fsck prints nothing, because it counts reflog entries as roots. With --no-reflogs it sees the orphan:

$ git fsck --no-reflogs
dangling commit 62ca4a145be65b0cea28498f7194f877b0742292
$ git reflog expire --expire=now --all
$ git fsck
dangling commit 62ca4a145be65b0cea28498f7194f877b0742292
$ git gc --prune=now
$ git fsck
$

A dangling commit is one nothing refers to. git fsck --lost-found writes them into .git/lost-found/ so you can inspect them, even without the reflog, as long as they haven’t been pruned. After the gc, there is nothing left. You will never do this by accident, because git gc ordinarily waits for the grace periods, but it explains why “I deleted it a year ago” is not recoverable.

The same mechanism is why a leaked secret doesn’t vanish when you rewrite history: see removing secrets from history.

What the video covers

System Crafters’ “How to Rescue Your Commits with Git Reflog” is about fourteen minutes and was published around 2025. From its title it walks through recovering commits from the reflog. I judge English from the title, description and channel only, and I could not read a transcript. I can’t tell how much it covers deleted branches or garbage collection, so the fsck and gc sections above are from my own runs. A shorter reflog video and a Modern SDE lesson that adds git gc and fsck are linked.

Where this fits

You can now recover from most mistakes. Next, using history to find when a mistake entered: finding a bug with bisect.

Check yourself

  1. 01You ran `git reset --hard HEAD~2` by mistake. How do you get the commits back?

    Run `git reflog`, find the entry from before the reset, and run `git reset --hard 'HEAD@{1}'` or the hash. The branch moves back to it.

  2. 02You deleted a branch with unmerged commits. What do you need to restore it?

    The commit hash of its tip. `git branch -D` prints it, and the reflog also has it. Then `git branch <name> <hash>` recreates the branch.

  3. 03Can the reflog save uncommitted changes you discarded?

    No. It tracks commits that were made. Work that was never committed or staged was never stored as an object.

  4. 04When does Git permanently delete an unreachable commit?

    After its reflog entries expire (90 days, or 30 for unreachable ones) and a `git gc` prunes it. Running `git reflog expire` plus `git gc --prune=now` does it immediately.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark