TExcelTAD
Home › Git › Git Reflog

Git Reflog

The reflog (reference log) is Git's local diary: every time HEAD or a branch moves, because of a commit, checkout, reset, merge, rebase or amend, Git writes down where it was. Commits that seem lost after a bad reset or rebase are almost always still reachable through it.

Reading the reflog

bash · try it yourself
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Version 1"
echo "v2" > app.txt && git commit -q -am "Version 2"
git switch -q -c experiment
echo "v3" > app.txt && git commit -q -am "Experimental version 3"
git switch -q main
git reflog
4466c34 HEAD@{0}: checkout: moving from experiment to main
532d579 HEAD@{1}: commit: Experimental version 3
4466c34 HEAD@{2}: checkout: moving from main to experiment
4466c34 HEAD@{3}: commit: Version 2
f28e6de HEAD@{4}: commit (initial): Version 1

Each line has the commit HEAD pointed to, a name you can use (HEAD@{2} = "where HEAD was two moves ago"), and what caused the move. The newest entry is on top.

Undoing a hard reset

This is the classic rescue. You reset main back two commits, then realise you needed them:

bash · try it yourself
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Version 1"
echo "v2" > app.txt && git commit -q -am "Version 2"
echo "v3" > app.txt && git commit -q -am "Version 3"
git reset -q --hard HEAD~2
git log --oneline
echo "--- oops. the reflog still knows:"
git reflog -3
git reset -q --hard HEAD@{1}
echo "--- restored:"
git log --oneline
f28e6de Version 1
--- oops. the reflog still knows:
f28e6de HEAD@{0}: reset: moving to HEAD~2
c4ead69 HEAD@{1}: commit: Version 3
4466c34 HEAD@{2}: commit: Version 2
--- restored:
c4ead69 Version 3
4466c34 Version 2
f28e6de Version 1

HEAD@{1} was the position before the reset, so resetting to it undoes the reset.

Branch reflogs and time-based names

Every branch has its own reflog too (git reflog show main), and entries can be addressed by time: main@{yesterday}, HEAD@{2.hours.ago}. git log -g shows the reflog in full log format.

bash · try it yourself
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "Version 1"
echo "v2" > app.txt && git commit -q -am "Version 2"
git commit -q --amend -m "Version 2 (final)"
git reflog show main
echo "--- the commit before the amend:"
git show -s --format="%h %s" main@{1}
4961e7e main@{0}: commit (amend): Version 2 (final)
4466c34 main@{1}: commit: Version 2
f28e6de main@{2}: commit (initial): Version 1
--- the commit before the amend:
4466c34 Version 2

Even the pre-amend version of the commit is still available as main@{1}.

Undoing a rebase

After a rebase, the old commits are no longer on the branch, but the reflog has the branch's pre-rebase position. Git also stores it in ORIG_HEAD right after a rebase, reset or merge:

bash · try it yourself
mkdir app && cd app && git init -q
echo "base" > base.txt && git add . && git commit -q -m "Base"
git switch -q -c feature && echo "f" > f.txt && git add . && git commit -q -m "Feature work"
git switch -q main && echo "m" > m.txt && git add . && git commit -q -m "Main work"
git switch -q feature
git rebase -q main
git log --oneline
echo "--- undo the rebase:"
git reset -q --hard ORIG_HEAD
git log --oneline
087b83f Feature work
96af146 Main work
c33fb2c Base
--- undo the rebase:
a769208 Feature work
c33fb2c Base

Limits of the reflog

  • It is local: it records what happened in your clone. It isn't pushed, and a fresh clone starts with an empty reflog.
  • Entries expire: unreachable ones after 30 days, others after 90 days by default (gc.reflogExpire), after which git gc may delete the commits.
  • It only knows about committed states. Changes you never committed (and never stashed or staged) cannot be recovered by Git.

Test yourself

What does the reflog record?

  1. All commits on the server
  2. Every movement of HEAD and branch pointers in your local repository
  3. Only merge commits
  4. Deleted files

You ran git reset --hard HEAD~3 by mistake. What is the quickest fix (if nothing else moved HEAD since)?

  1. git revert HEAD~3
  2. git reset --hard HEAD@{1}
  3. git pull
  4. The commits are gone

Can the reflog recover changes that were never committed?

  1. Yes, always
  2. No, it only tracks committed states that refs pointed to
  3. Only on Windows
  4. Only if pushed