Lessons · Git · everywhere HEAD has been
Everywhere HEAD has been
git log walks backwards from where you are. git reflog lists where you have BEEN, so a commit you have moved away from is in one and invisible in the other.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
After a reset, a bad rebase or a checkout that went sideways, the reflog is the only thing that still names the commit you want.
How to think about it
Read the entries as moves rather than history. HEAD@{1} is one move ago, whatever branch that was on.
Worked example
git log --onelineOne commit. As far as this branch is concerned the other one never happened.
git reflogTwo moves and the commit, still named.
git show -s --oneline HEAD@{1}One move ago: the commit the reset moved off.git branch rescue HEAD@{1}A name for it, which costs nothing and changes nothing else.git log --oneline rescueBoth commits, on a branch you can look at before deciding.
Your turn
Give the position HEAD was in one move ago a branch called rescue.
git branch rescue
Try it in a real repository
The trap
Reading HEAD@{1} as HEAD~1. One counts moves you made, the other counts commits in the history, and after a reset they are different commits.