Dismissile Dismissile - 6 months ago 49
Git Question

HEAD~ for a merge commit

In Git, you can refer to the commit before HEAD using the shorthand HEAD~, and two before using HEAD~2, etc.

I have a repository that has a merge commit like the following:

\ /

HEAD = F, HEAD~ points to B, and HEAD~2 points to A. With a merge commit like this, is there a shorthand that would point to E?


Yes; the ~ specifies the generation, but you can use ^ to specify the parent number, in the case of a merge.

git show HEAD^2

Will show the 2nd parent.

(The distinction is especially subtle since without a numeric argument both ~ and ^ show the same thing, the first parent of HEAD. This is because both ~ and ^ default to 1 without a numeric parameter. So they show the first parent (by depth) and the first parent (by breadth) which are of course the same.)