Sub topic 6
Checking an entry before you restore it
This chapter assumes you can read git reflog output and use selectors such as HEAD@{1}. It also assumes you know that branches and HEAD are pointers to commits.
Restoring from the reflog usually means moving a branch with git reset --hard. That command rewrites your working tree, so a wrong entry costs you time and may cost uncommitted changes. Reading an entry is free and changes nothing, so check first.
Look at the commit
git show accepts a reflog selector anywhere it accepts a commit.
git show --stat HEAD@{1}The output gives the author, date, message, and the files that commit changed. If the message and files match the work you lost, you probably have the right entry. If not, try HEAD@{2} or the next one.
In PowerShell, quote the selector ('HEAD@{1}'), because the braces have a special meaning there.
Look at the history behind it
One commit can look right while its ancestors are wrong. List the commit and the four before it:
git log --oneline -5 HEAD@{1}You should see the commits you remember making, in the order you made them.
Compare it with where you are now
To see what a restore would change, compare the entry with your current commit:
git diff --stat HEAD HEAD@{1}Empty output means both commits have identical contents, so restoring changes nothing. A long list of files means the restore moves a lot, so confirm that is what you want.
Resolve the selector to a hash
Selectors move. Every new reflog entry pushes the older ones down, so HEAD@{1} becomes HEAD@{2} after your next commit, checkout, or reset. Convert the selector to a fixed hash once you are happy with it:
git rev-parse HEAD@{1}Write the hash down. A hash always names the same commit, so you can use it in later commands without the number shifting under you.
Try it on a throwaway branch
To go beyond reading, create a branch at the entry. This does not touch your current branch or working tree.
git branch recovered HEAD@{1}
git log --oneline recoveredIf the branch is correct, keep it, merge it, or reset to it. If it is wrong, delete it with git branch -D recovered. The commit stays in the reflog either way.
| Command | What it tells you | Changes anything? |
|---|---|---|
| `git show --stat <entry>` | Message, author, files changed | No |
| `git log --oneline -5 <entry>` | Recent history behind the entry | No |
| `git diff --stat HEAD <entry>` | What a restore would change | No |
| `git rev-parse <entry>` | A fixed hash for the entry | No |
| `git branch <name> <entry>` | Creates a branch to examine | Adds a branch only |
Exercise
You run git show --stat HEAD@{1} and the commit looks right. You then make one new commit before restoring. Why might git reset --hard HEAD@{1} now do the wrong thing, and what should you have done?
Show answer
The new commit added a reflog entry, so HEAD@{1} now points to the commit you were on before, not the one you inspected. You should have run git rev-parse HEAD@{1} right after inspecting it and reset to the resulting hash.