Lessons · Git · HEAD: where you are standing
Where HEAD points
HEAD names the commit you have checked out, normally through a branch. HEAD~1 is one commit back, HEAD~2 two back.
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
Most commands are relative to HEAD: diff HEAD~1, reset --soft HEAD~1, show HEAD. Knowing what HEAD is makes those commands readable instead of magic.
How to think about it
Picture two arrows: HEAD points at a branch, the branch points at a commit. Commit, and the branch moves; switch, and HEAD moves to another branch.
Worked example
git log --oneline -3Three commits; the top one is where HEAD is.
git show -s --oneline HEADThe current commit: HEAD is a name for it.
git show -s --oneline HEAD~1One commit back.
cat .git/HEADref: refs/heads/main. HEAD points at the branch; the branch points at the commit.
Your turn
Compare the last commit with the one before it.
git diff HEAD
Try it in a real repository
The trap
HEAD~1 is the first parent. On a merge commit, HEAD^2 is the second parent, the branch that was merged in.
Practise HEAD: where you are standing on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.