Skip to content
$ lynuxpro
← Git

LESSON 15 OF 21 · ADVANCED · 15 MIN

Recovering lost work with reflog

HOW IT WORKSThe reflog remembers where HEAD has been

c3your workreset --hard HEAD~2oopsc1branch now heregit reflogHEAD@{1} = c3Branch restoredreset --hard HEAD@{1}
1/4

A bad reset or rebase can make commits disappear from git log.

Sooner or later you will run git reset --hard on the wrong commit, delete a branch you still needed, or finish a rebase and realise it went wrong. The commits seem gone: git log no longer shows them. They almost never are. Git keeps a private diary called the reflog that records every position HEAD and your branches have had. This lesson shows how to read it and bring lost commits back.

In simple words

  • Reflog: Git's private diary of everywhere HEAD has pointed. It lets you find commits after a bad reset or rebase.
  • Dangling commit: a commit no branch points to any more. It still exists for a while and can be recovered.

Why commits do not disappear

When you reset, rebase, amend or delete a branch, Git does not delete commits. It only moves pointers. The old commits stay in the object database, unreachable from any branch, until garbage collection removes them, which by default happens no earlier than 30 days for unreachable commits (90 days for reflog entries of reachable ones). Until then, you just need their hash, and the reflog has it.

Lose some work

git config --global user.name "Leela Kumar"mkdir reflog-labcd reflog-labgit initprintf 'v1\n' > app.txtgit add app.txtgit commit -m "Version 1"printf 'v2\n' > app.txtgit commit -am "Version 2"printf 'v3\n' > app.txtgit commit -am "Version 3: two days of work"git log --oneline
SHELLbashutf-8 · 12L
9c3b2a1 (HEAD -> main) Version 3: two days of work8b2a1f0 Version 27a1f0e9 Version 1
OUTPUTterminalutf-8 · 3L

Now the mistake: you meant to reset one file, but reset the branch two commits back:

git reset --hard HEAD~2git log --onelinecat app.txt
SHELLbashutf-8 · 3L
HEAD is now at 7a1f0e9 Version 17a1f0e9 (HEAD -> main) Version 1v1
OUTPUTterminalutf-8 · 3L

Two days of work, apparently gone.

Read the reflog

git reflog
SHELLbashutf-8 · 1L
7a1f0e9 HEAD@{0}: reset: moving to HEAD~29c3b2a1 HEAD@{1}: commit: Version 3: two days of work8b2a1f0 HEAD@{2}: commit: Version 27a1f0e9 HEAD@{3}: commit (initial): Version 1
OUTPUTterminalutf-8 · 4L

Every line is a position HEAD had, newest first, with the action that moved it. HEAD@{1} means "where HEAD was one move ago". The commit you lost is right there.

Bring it back

Point the branch back at the lost commit:

git reset --hard HEAD@{1}git log --onelinecat app.txt
SHELLbashutf-8 · 3L
HEAD is now at 9c3b2a1 Version 3: two days of work9c3b2a1 (HEAD -> main) Version 3: two days of work8b2a1f0 Version 27a1f0e9 Version 1v3
OUTPUTterminalutf-8 · 5L

Everything is back. Other ways to recover, depending on what you need:

GoalCommand
Move the current branch backgit reset --hard HEAD@{n} (or the hash)
Keep the current state and save the lost commit on a new branchgit branch rescue HEAD@{n}
Just look at itgit show HEAD@{n} or git switch --detach HEAD@{n}
Take one file from itgit restore --source=HEAD@{n} file
Re-apply one lost commitgit cherry-pick <hash>

Creating a rescue branch is the safest choice when you are not sure: it changes nothing else.

Recover a deleted branch

git switch -c feature/experimentprintf 'experiment\n' > exp.txtgit add exp.txtgit commit -m "Try an experiment"git switch maingit branch -D feature/experimentgit reflog -n 3
SHELLbashutf-8 · 7L
Deleted branch feature/experiment (was 5e4d3c2).9c3b2a1 HEAD@{0}: checkout: moving from feature/experiment to main5e4d3c2 HEAD@{1}: commit: Try an experiment9c3b2a1 HEAD@{2}: checkout: moving from main to feature/experiment
OUTPUTterminalutf-8 · 4L

Git even printed the hash when deleting (was 5e4d3c2). Recreate the branch from the reflog entry:

git branch feature/experiment HEAD@{1}git log --oneline feature/experiment -n 1
SHELLbashutf-8 · 2L

Undo a rebase that went wrong

During a rebase Git records ORIG_HEAD and many reflog entries. To go back to how the branch was before the rebase started:

git reflog                     # find the entry just before "rebase (start)"git reset --hard HEAD@{5}      # or: git reset --hard ORIG_HEAD right after the rebase
SHELLbashutf-8 · 2L

Branch reflogs

Each branch has its own reflog too, which is handy when HEAD has moved around a lot:

git reflog show maingit log -g --oneline feature/login      # reflog as a logmain@{yesterday}                        # where main was yesterdaymain@{2.hours.ago}
SHELLbashutf-8 · 4L

What the reflog cannot save

  • Uncommitted changes destroyed by git reset --hard, git restore or git clean -f. They were never commits, so there is nothing to recover. Commit or stash before risky commands.
  • Work from another machine or clone. The reflog is local: it is not pushed or cloned.
  • Very old entries after garbage collection.

Two safety habits help: run git stash or make a quick commit before experimenting, and create a backup branch (git branch backup/before-rebase) before a complicated rebase.

Real-world scenario: the rebase at the end of a long day

At 7 p.m., a developer finished a messy interactive rebase of a 15-commit branch and force-pushed it. The next morning the tests failed: a dropped line in the todo list had deleted an important commit. git reflog showed rebase (start) at the previous evening; the entry just before it was the branch before the rebase. git branch before-rebase HEAD@{23} restored the old version alongside the new one, git cherry-pick brought the lost commit across, and the branch was force-pushed again with --force-with-lease. Total time: ten minutes.

Common mistakes

  • Assuming commits are gone and redoing the work. Check the reflog first.
  • Running git gc --prune=now in a panic, which really does delete unreachable commits.
  • Recovering with reset --hard while you have uncommitted changes, destroying those instead. Use a rescue branch.
  • Expecting the reflog on a fresh clone to show another person's history.

Practice

  1. Make three commits, then git reset --hard HEAD~2. Recover them with git reflog and git reset --hard HEAD@{1}.
  2. Create a branch with a commit, delete it with -D, and recreate it from the reflog.
  3. Save a lost commit on a rescue branch with git branch rescue HEAD@{n} instead of resetting.

Cheat sheet

git reflog [-n 20]               where HEAD has beengit reflog show branch           a branch's own historyHEAD@{n}   main@{yesterday}      reflog referencesgit reset --hard HEAD@{n}        move branch back to that pointgit branch rescue HEAD@{n}       save it on a new branch (safest)git show HEAD@{n}                inspectgit cherry-pick <hash>           re-apply one lost commitgit reset --hard ORIG_HEAD       undo the last rebase/merge/reset
OUTPUTterminalutf-8 · 8L

Key takeaways

  • Reset, rebase, amend and branch deletion only move pointers; old commits survive for weeks.
  • The reflog records every position of HEAD, so lost commits can be found by hash or HEAD@{n}.
  • Recover by resetting to the entry, or more safely by creating a rescue branch there.
  • Uncommitted changes cannot be recovered; commit or stash before risky operations.