Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HEAD~ vs HEAD^ vs HEAD@{} also known as tilde vs caret vs at sign

Tags:

git

HEAD is a pointer at the current branch. I have seen a variety of notations for ancestors of HEAD including

  • HEAD~2
  • HEAD^2
  • HEAD@{2}
  • HEAD~~
  • HEAD^^

What does each of the above mean, exactly? Where is the documentation for this?

like image 678
Shaun Luttin Avatar asked Nov 06 '14 16:11

Shaun Luttin


People also ask

What does Head @{ 3 refer to in Git?

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 is tilde in Git?

The ~(tilde) and ^(caret) symbols are used to point to a position relative to a specific commit. The symbols are used together with a commit reference, typically HEAD or a commit hash.

What does Carrot mean in Git?

The caret refers to the parent of a particular commit. E.g. HEAD^ refers to the parent of the current HEAD commmit. (also, HEAD^^ refers to the grandparent).

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 .


2 Answers

From the docs here.

  • HEAD~2 : 2 commits older than HEAD
  • HEAD^2 : the second parent of HEAD, if HEAD was a merge, otherwise illegal
  • HEAD@{2} : refers to the 3rd listing in the overview of git reflog
  • HEAD~~ : 2 commits older than HEAD
  • HEAD^^ : 2 commits older than HEAD

If HEAD was a merge, then

  • first parent is the branch into which we merged,
  • second parent is the branch we merged.

Some Combinations and Synonyms

First Parent    First Grandparent    Second Parent    Second Grandparent  HEAD~ HEAD^ HEAD~1          HEAD~2               HEAD^2           HEAD^2~         HEAD^1          HEAD^^                                HEAD^2^  
like image 77
Tim Avatar answered Oct 08 '22 09:10

Tim


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. And so on.

ref^ is shorthand for ref^1 and means the commit's first parent. But where the two differ is that ref^2 means the commit's second parent (remember, commits can have two parents when they are a merge).

The ^ and ~ operators can be combined.

Here's a diagram showing how to reference various commits using HEAD as the starting point.
enter image description here

src

like image 33
Premraj Avatar answered Oct 08 '22 08:10

Premraj