Sub topic 3
Reading git reflog output
This chapter assumes you know how to commit, branch, and switch branches, and that you understand a branch is a movable pointer to a commit. It covers how to read what git reflog prints so you can pick the right entry to return to.
What one line contains
Run the command in any repository where you have been working:
git reflog
# 3f2a9c1 HEAD@{0}: reset: moving to HEAD~2
# 9d4e7b8 HEAD@{1}: commit: Add export feature
# c81b0aa HEAD@{2}: commit: Fix parser bug
# 5e6f1d2 HEAD@{3}: commit: Start parser workEach line has three parts:
- A short commit hash. This is the commit that
HEADpointed to after the action finished. - A selector such as
HEAD@{1}. It names the entry by how many moves ago it happened. - A description. It starts with the kind of action (
commit,reset,checkout,rebase,merge) and then gives details.
The newest entry is at the top. HEAD@{0} is where HEAD is now, and higher numbers go further back in time.
Reading the example
Read from the bottom up to follow the story in the order it happened:
5e6f1d2is a commit named "Start parser work".c81b0aais the next commit, "Fix parser bug".9d4e7b8is "Add export feature".- The
reset: moving to HEAD~2line showsHEADmoving back two commits, to3f2a9c1.
The hash on each line is the state after that action. So the line for the reset shows where you ended up, not what you discarded. The commits you lost are on the lines below it. The one to return to is 9d4e7b8 at HEAD@{1}, because that was HEAD just before the reset.
Common action descriptions
| Description starts with | What happened |
|---|---|
| `commit:` | You made a commit. The message follows. |
| `commit (amend):` | You replaced the previous commit. |
| `checkout: moving from A to B` | You switched branches or checked out a commit. |
| `reset: moving to X` | You ran `git reset`. |
| `rebase (finish):` | A rebase completed. |
| `merge X:` | You merged branch X. |
Use these labels to find the last good state. Look for the line just before the action that went wrong.
Common reading errors
- Choosing the top entry. The top entry is the current state. After a mistake, it is usually the bad one.
- Counting by commits. The number in
HEAD@{n}counts reflog moves, not commits in the history. A checkout or a reset uses up a number just as a commit does.HEAD~2andHEAD@{2}often point to different commits. - Assuming numbers stay fixed. Every new action pushes the older entries down by one. Copy the hash if you need a stable reference.
- Skipping the description. Two entries can look alike by hash. The description tells you which action created each one.
Long output
In an active repository the output is long. Show only the newest entries:
git reflog -10
git reflog | grep 'rebase'The first command limits the count. The second filters for one kind of action. Both give you a shorter list to read.
Exercise
A reflog shows a1b2c3d HEAD@{0}: reset: moving to HEAD~1, then e4f5a6b HEAD@{1}: commit: Add login form. Which hash holds the commit that the reset discarded, and how do you know?
Show answer
e4f5a6b. The hash on each line is where HEAD pointed after that action. The reset line shows the new position, a1b2c3d. The line just below it, HEAD@{1}, shows the commit HEAD was on before the reset.
Exercise
You run git reflog and note that your lost commit is at HEAD@{3}. You then make one new commit before restoring it. Is the lost commit still at HEAD@{3}?
Show answer
No. The new commit adds an entry at HEAD@{0}, so the lost commit moves to HEAD@{4}. Use the hash instead, or run git reflog again.