21

In git, what is the difference between the following?

  • HEAD
  • HEAD^
  • HEAD~1
  • HEAD~2

And how do they relate to master? So is there a MASTER^, MASTER~1??

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Patoshi パトシ
  • 21,707
  • 5
  • 29
  • 47

1 Answers1

26

HEAD is a synonym for the most recent commit on your current branch, whatever it is.

HEAD^ (or HEAD^1) means the first parent of HEAD. A merge commit has multiple parents, so HEAD^2 refers to the second immediate parent of HEAD that was involved in the merge that created HEAD.

HEAD~1 is the same as HEAD~. In this case, it is synonymous with HEAD^. To see the difference, consider that HEAD~2 is the grandparent of HEAD. Using ~ goes back generations.

If you happen to be on the master branch, then HEAD refers to master. If you are on branch topic/foo, then it refers to that branch while you are on it.

Case matters with git. MASTER^ or MASTER~1 is likely to produce errors of the form

fatal: ambiguous argument 'MASTER~1': unknown revision or path not
in the working tree.

But master^ and master~1 are meaningful.

See the git rev-parse documentation for full details of the many ways you can address commits.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245