Git Reflog

Updated · 11 min read
mediumGit 2.53gitgit-reflogrecoveryorig-headinterview

The reflog (reference log) is a record that Git keeps of every position HEAD and each branch have had in your repository: every commit, switch, merge, rebase, reset, amend and pull, with the commit it pointed to before and after. The command git reflog shows it. It is used to find commits that are no longer on any branch, which is why it is the first tool to reach for when work seems lost.

It works like the call history on a phone. Your contacts list (the branches) only shows people you saved. The call history shows everyone you talked to recently, saved or not. If you forgot to save a number, you scroll back through the history and find it.

Syntax

Terminal
git reflog [show] [<ref>]
CommandDescription
git reflogIt is used to show where HEAD has been, newest first.
git reflog show <branch>It is used to show where one branch has pointed.
git reflog --date=isoIt is used to show the time of each entry.
git log -g --onelineIt is used to walk the reflog with git log formatting.
HEAD@{n}, <branch>@{n}It is used to name the position n moves ago (see "Naming Commits").
@{-1}It is used to name the branch you were on before the last switch.

Features of the Reflog

  • Local only. Each repository has its own reflog. It is never pushed or cloned.
  • Records actions, not just commits. Switches, resets and rebases appear, even when they create no commit.
  • Temporary. Entries expire: by default after 90 days, or 30 days for entries pointing to commits that no branch reaches any more.
  • Always on. In normal repositories, Git keeps it automatically. You never have to turn it on.

Reading the Reflog

Navika makes a few commits, switches branches and back, and amends:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git switch --quiet -c feature/receipts
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add hostel"
git commit --quiet --amend -m "Add the hostel fee"
git reflog
Output
7bbd2e4 (HEAD -> main) HEAD@{0}: commit (amend): Add the hostel fee
22a6bc0 HEAD@{1}: commit: Add hostel
d363415 (feature/receipts) HEAD@{2}: checkout: moving from feature/receipts to main
d363415 (feature/receipts) HEAD@{3}: checkout: moving from main to feature/receipts
d363415 (feature/receipts) HEAD@{4}: commit: Add the late fee
6b651a0 HEAD@{5}: commit (initial): Add the term fee

Explanation: each line is one movement of HEAD, newest at the top:

PartMeaning
7bbd2e4The commit HEAD pointed to after that action
HEAD@{0}Its position in the reflog: 0 is now, 1 is one move ago, and so on
commit (amend): ...The action and its message

Reading from the bottom up, you can follow the whole session: the first commit, the second, a switch to feature/receipts and back, the hostel fee commit, and the amend that replaced it. Note that the original "Add hostel" commit, 22a6bc0, is still listed, although no branch contains it anymore.

Every position of HEAD, newest first

A Branch's Own Reflog

Each branch has its own reflog too, recording only that branch's moves:

Terminal
git reflog show main
Output
7bbd2e4 (HEAD -> main) main@{0}: commit (amend): Add the hostel fee
22a6bc0 main@{1}: commit: Add hostel
d363415 (feature/receipts) main@{2}: commit: Add the late fee
6b651a0 main@{3}: commit (initial): Add the term fee

main@{1} is where main pointed before its last move: the "Add hostel" commit, before the amend.

Example 1: Undo a git reset --hard

Navika means to undo one commit, but types HEAD~2:

Terminal
git reset --hard HEAD~2
git log --oneline
Output
HEAD is now at 6b651a0 Add the term fee
6b651a0 (HEAD -> main) Add the term fee

Two commits gone from main. The reflog still knows where HEAD was just before:

Terminal
git reflog -3
Output
6b651a0 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2
7bbd2e4 HEAD@{1}: commit (amend): Add the hostel fee
22a6bc0 HEAD@{2}: commit: Add hostel

HEAD@{1} is the position before the reset, "Add the hostel fee". Resetting to it brings everything back:

Terminal
git reset --hard HEAD@{1}
git log --oneline
Output
HEAD is now at 7bbd2e4 Add the hostel fee
7bbd2e4 (HEAD -> main) Add the hostel fee
d363415 (feature/receipts) Add the late fee
6b651a0 Add the term fee

Explanation: git reset --hard didn't delete any commit. It only moved main. The commits were still in the repository, and the reflog showed where they were.

Example 2: Undo a Rebase

Navika rebases feature/receipts, which has two commits, onto main, and then realizes the team wanted it merged, not rebased:

Terminal
git switch --quiet feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
echo "Print after payment" >> receipts.txt
git commit --quiet -am "Print a receipt after payment"
git rebase --quiet main
git log --oneline -4
Output
6947afd (HEAD -> feature/receipts) Print a receipt after payment
2a96b64 Add receipt numbers
7bbd2e4 (main) Add the hostel fee
d363415 Add the late fee

The branch's reflog shows both versions:

Terminal
git reflog show feature/receipts
Output
6947afd (HEAD -> feature/receipts) feature/receipts@{0}: rebase (finish): refs/heads/feature/receipts onto 7bbd2e43db53f2c93825d04ca9639800c86c2810
03a5595 feature/receipts@{1}: commit: Print a receipt after payment
c9d620d feature/receipts@{2}: commit: Add receipt numbers
d363415 feature/receipts@{3}: branch: Created from HEAD

feature/receipts@{1} is the branch as it was before the rebase. Right after a rebase, ORIG_HEAD points there too:

Terminal
git reset --hard feature/receipts@{1}
git log --oneline --graph --all
Output
HEAD is now at 03a5595 Print a receipt after payment
* 03a5595 (HEAD -> feature/receipts) Print a receipt after payment
* c9d620d Add receipt numbers
| * 7bbd2e4 (main) Add the hostel fee
|/
* d363415 Add the late fee
* 6b651a0 Add the term fee

The branch is back on its original base, with its original commit IDs, as if the rebase never happened.

ORIG_HEAD and Other Special References

Besides the reflog, Git writes a few one-off references while it works. They are files in .git/:

ReferenceWritten byPoints to
ORIG_HEADreset, merge, rebase, pullWhere HEAD was before the command moved it far
FETCH_HEADfetch, pullWhat was just fetched
MERGE_HEADmerge while a conflict is openThe commit being merged
CHERRY_PICK_HEADcherry-pick while a conflict is openThe commit being copied
REVERT_HEADrevert while a conflict is openThe commit being reverted

ORIG_HEAD is the quickest undo right after one big move. The reflog is the complete history, for anything older.

Common Mistakes

Confusing HEAD@{2} with HEAD~2. HEAD~2 is two commits back in history. HEAD@{2} is where HEAD was two actions ago. After resets and rebases they are very different.

Reading the reflog upside down. It is newest first, like git log. The mistake you want to undo is near the top. The state before it is the line below it.

Expecting the reflog on the server or in a new clone. It is local only.

Looking only at HEAD's reflog. A branch's own reflog (git reflog show <branch>) is shorter and shows only that branch's moves.

Running cleanup commands while recovering. git gc --prune=now or git reflog expire can delete exactly the commits you are trying to find. Recover first.

Under the Hood

The reflog is a set of plain text files in .git/logs/: .git/logs/HEAD for HEAD, and .git/logs/refs/heads/<branch> for each branch. Every time a reference changes, Git appends a line with the old ID, the new ID, your identity, the time and a short description:

Terminal
tail -2 .git/logs/HEAD
Output
6947afdd1667ad52845c77c2606f8813d5cb0edf 6947afdd1667ad52845c77c2606f8813d5cb0edf Navika Anamaneni <navika@example.com> 1767630720 -0600	rebase (finish): returning to refs/heads/feature/receipts
6947afdd1667ad52845c77c2606f8813d5cb0edf 03a5595da74494e780b16d8a359a8fd8ee1bc1c3 Navika Anamaneni <navika@example.com> 1767631020 -0600	reset: moving to feature/receipts@{1}

git reflog just reads these files, newest line first, and numbers them. Because the old ID is recorded too, every move can be reversed. Deleting a branch deletes its own reflog file, which is why HEAD's reflog matters for recovering deleted branches, as the next lesson shows.

Interview Corner

What is the reflog?

A local log of every position HEAD and each branch have pointed to, including commits, switches, resets, rebases and amends. It lets you find commits that are no longer reachable from any branch.

How do you undo git reset --hard?

Find the previous position with git reflog and run git reset --hard HEAD@{n} for the entry before the reset (often HEAD@{1}), or use ORIG_HEAD right after. Committed work comes back. Uncommitted changes that were overwritten can't be recovered this way.

What is the difference between HEAD~1 and HEAD@{1}?

HEAD~1 is the first parent of the current commit, from the commit history. HEAD@{1} is where HEAD pointed before the last action, from the reflog.

Is the reflog shared when you push?

No. It is local to each repository and isn't pushed, fetched or cloned.

How long do reflog entries last?

By default, 90 days (gc.reflogExpire), or 30 days for entries pointing to commits that are no longer reachable (gc.reflogExpireUnreachable). Expired entries are removed during garbage collection.

Practice

Challenge: find the old message. On main, the hostel fee commit was amended at the start of the lesson. Use main's reflog to find the commit before the amend, and print its message, without changing any branch.

What does Git print? Before you open the solution, decide which reflog name points to it.

Show solution
Terminal
git switch --quiet main
git reflog show main
git log -1 --format=%s main@{3}
Output
7bbd2e4 (HEAD -> main) main@{0}: reset: moving to HEAD@{1}
6b651a0 main@{1}: reset: moving to HEAD~2
7bbd2e4 (HEAD -> main) main@{2}: commit (amend): Add the hostel fee
22a6bc0 main@{3}: commit: Add hostel
d363415 main@{4}: commit: Add the late fee
6b651a0 main@{5}: commit (initial): Add the term fee
Add hostel

main's reflog lists the hard reset and the reset back, then the amend, then "Add hostel". Counting from the top, the commit before the amend is main@{3}, and its message is "Add hostel". Reading the reflog changes nothing: no branch moved.

Checkpoint. In a practice repository, make three commits, run git reset --hard HEAD~3, and get them back with the reflog. Then rebase a branch and undo it with <branch>@{1}, and once more with ORIG_HEAD.

Quick check: Git reflog

4 questions
1What does git reflog show?
2After git reset --hard HEAD~2 by mistake, which command brings the commits back?
3Is the reflog included when you clone a repository?
4Which can the reflog NOT recover?

Key Takeaways

Quick reference

CommandIt is used to
git reflogShow where HEAD has been
git reflog show <branch>Show where a branch has been
git reflog --date=isoShow the time of each entry
git reset --hard HEAD@{n}Go back to an earlier position
git branch <name> HEAD@{n}Save an earlier position as a branch
git reset --hard ORIG_HEADUndo the last reset, merge or rebase

Was this tutorial helpful?

Lesson 64 of 106