Git's HEAD
In git literature is common to come across HEAD
, HEAD^
, and HEAD~N
. Unfortunately, they usually don’t come with an explanation.
The Tree
G H I J
\ / \ /
D E F
\ | / \
\ | / |
\|/ |
B C
\ /
\ /
A
The illustration above, by Jon Loeliger, depicts a commit history tree. We can think about it as: (from A
’s view point)
A = HEAD = HEAD^0
B = HEAD^ = HEAD^1 = HEAD~1
C = HEAD^2 = HEAD^2
D = HEAD^^ = HEAD^1^1 = HEAD~2
E = B^2 = HEAD^^2
F = B^3 = HEAD^^3
G = HEAD^^^ = HEAD^1^1^1 = HEAD~3
H = D^2 = B^^2 = HEAD^^^2 = HEAD~2^2
I = F^ = B^3^ = HEAD^^3^
J = F^2 = B^3^2 = HEAD^^3^2
Where ^N
stands for the Nth parent commit. That is, those commits immediately before it, regardless of branch of origin. ^1
can be shorten to ^
. Each parent is indexed from left to right, and relative to it’s child.
~N
abbreviates N contiguous ^1
parents. Which means, that set of commits forms a branch; like G-D-B-A above. We can think of it as HEAD
’s N previous commits.
For more info read the specifying revision section on git-rev-parse(1).