What's the difference between HEAD ^ and HEAD ~?

When I specify ancestor commit objects in Git, I'm confused between HEAD ^ and HEAD ~.

Both have a numbered version, such as HEAD^3 and HEAD~2.

In my opinion, they look very similar or the same, but is there any difference between the tilde and the caret?

#1 building

The difference between HEAD ^ and HEAD ~ is described in the figure (through Jon Loeliger) found above http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html .

This document may be a little obscure for beginners, so I copied the following illustrations:

G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A
A =      = A^0
B = A^   = A^1     = A~1
C = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2

#2 building

It's worth noting that git also has syntax for tracking "from where you are" / back now "- for example, HEAD@{1} will refer to the location you jump to to the new commit location.

Basically, the HEAD @ {} variable captures the history of the HEAD movement. You can use the GIT reflog command to view git reflog to decide to use a specific HEAD.

Example:

0aee51f HEAD@{0}: reset: moving to HEAD@{5}
290e035 HEAD@{1}: reset: moving to HEAD@{7}
0aee51f HEAD@{2}: reset: moving to HEAD@{3}
290e035 HEAD@{3}: reset: moving to HEAD@{3}
9e77426 HEAD@{4}: reset: moving to HEAD@{3}
290e035 HEAD@{5}: reset: moving to HEAD@{3}
0aee51f HEAD@{6}: reset: moving to HEAD@{3}
290e035 HEAD@{7}: reset: moving to HEAD@{3}
9e77426 HEAD@{8}: reset: moving to HEAD@{3}
290e035 HEAD@{9}: reset: moving to HEAD@{1}
0aee51f HEAD@{10}: reset: moving to HEAD@{4}
290e035 HEAD@{11}: reset: moving to HEAD^
9e77426 HEAD@{12}: reset: moving to HEAD^
eb48179 HEAD@{13}: reset: moving to HEAD~
f916d93 HEAD@{14}: reset: moving to HEAD~
0aee51f HEAD@{15}: reset: moving to HEAD@{5}
f19fd9b HEAD@{16}: reset: moving to HEAD~1
290e035 HEAD@{17}: reset: moving to HEAD~2
eb48179 HEAD@{18}: reset: moving to HEAD~2
0aee51f HEAD@{19}: reset: moving to HEAD@{5}
eb48179 HEAD@{20}: reset: moving to HEAD~2
0aee51f HEAD@{21}: reset: moving to HEAD@{1}
f916d93 HEAD@{22}: reset: moving to HEAD@{1}
0aee51f HEAD@{23}: reset: moving to HEAD@{1}
f916d93 HEAD@{24}: reset: moving to HEAD^
0aee51f HEAD@{25}: commit (amend): 3rd commmit
35a7332 HEAD@{26}: checkout: moving from temp2_new_br to temp2_new_br
35a7332 HEAD@{27}: commit (amend): 3rd commmit
72c0be8 HEAD@{28}: commit (amend): 3rd commmit

An example might be that I made local commits a - > b - > C - > D, and then I gave up two commits to check my code git reset HEAD~2, and then I want to move the HEAD back to d-git reset HEAD @ {1}.

#3 building

HEAD ^ ^ is the same as HEAD ~ 3, select the third submission before HEAD

HEAD ^ 2 specifies the second head in the merge commit

#4 building

^<n> The format allows you to select the nth parent of the submission (associated with the merge). ~<n> Format allows you to choose the nth ancestor submission that always follows the first parent. For some examples, see git-rev-parse Documents.

#5 building

  • HEAD ~ specifies the first parent on the branch

  • HEAD ^ allows you to select a specific parent to submit

An example:

If you want to follow a side branch, you must specify a similar

master~209^2~15

Keywords: git

Added by m3rajk on Wed, 25 Dec 2019 10:54:35 +0200