Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference from HEAD, HEAD^, and HEAD~1?

Tags:

git

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??

like image 667
Patoshi パトシ Avatar asked Jan 06 '14 16:01

Patoshi パトシ


People also ask

What is head Head 1 Head 2 git?

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.

What's the difference between head and head in git?

The difference between HEAD^ (Caret) and HEAD~ (Tilde) is how they traverse history backwards from a specified starting point, in this particular case HEAD .

What does Head@ 3 refer to?

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.

What does head mean in git?

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 .


1 Answers

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.

like image 123
Greg Bacon Avatar answered Sep 16 '22 14:09

Greg Bacon