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
??
HEAD~1 refers to the commit's first parent. HEAD~2 refers to the first parent of the commit's first parent. ^<n> refers to the the <n>th parent. HEAD^1 refers to the commit's first parent. HEAD^2 refers to the commit's second parent.
The difference between HEAD^ (Caret) and HEAD~ (Tilde) is how they traverse history backwards from a specified starting point, in this particular case HEAD .
git reference suffixes (^N, ~N, @{... }) ref~ is shorthand for ref~1 and means the commit's first parent. ref~2 means the commit's first parent's first parent. ref~3 means the commit's first parent's first parent's first parent.
When working with Git, only one branch can be checked out at a time - and this is what's called the "HEAD" branch. Often, this is also referred to as the "active" or "current" branch. Git makes note of this current branch in a file located inside the Git repository, in . git/HEAD .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With