Hone

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 --oneline
One commit. As far as this branch is concerned the other one never happened.
git reflog
Two 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 rescue
Both 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 

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.

Practise everywhere HEAD has been on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.