当我在Git中指定一个祖先提交对象时,我混淆了HEAD^和HEAD~。
两者都有“编号”版本,如HEAD^3和HEAD~2。
在我看来它们非常相似或相同,但是波浪号和插入符号之间有什么不同吗?
当我在Git中指定一个祖先提交对象时,我混淆了HEAD^和HEAD~。
两者都有“编号”版本,如HEAD^3和HEAD~2。
在我看来它们非常相似或相同,但是波浪号和插入符号之间有什么不同吗?
当前回答
HEAD^^^与HEAD~3相同,选择HEAD之前的第三次提交
HEAD^2指定合并提交中的第二个头
其他回答
~和^本身都是指提交的父节点(~~和^^都是指祖父节点提交,等等),但当它们与数字一起使用时,它们的含义有所不同:
~2表示在层次结构中向上两层,如果一个提交有多个父级,则通过第一个父级 ^2表示第二个父节点,其中提交有多个父节点(也就是说,因为它是一个merge)
这些可以组合起来,所以HEAD~2^3意味着HEAD的祖父提交的第三个父提交。
HEAD^^^与HEAD~3相同,选择HEAD之前的第三次提交
HEAD^2指定合并提交中的第二个头
简单地说,对于亲子关系的第一级(祖先,继承,世系等),HEAD^和HEAD~都指向同一个提交,它位于HEAD(提交)之上的一个父级。
此外,HEAD^ = HEAD^1 = HEAD~ = HEAD~1。但是HEAD^^ != HEAD^2 != HEAD~2。然而头^^ =头~2。继续读下去。
在第一级亲子关系之外,事情变得更加棘手,特别是如果工作分支/主分支有合并(来自其他分支)。还有一个插入符号的语法问题,HEAD^^ = HEAD~2(它们是等价的)但是HEAD^^ != HEAD^2(它们是完全不同的两个东西)。
每个/插入符号指的是HEAD的第一个父代,这就是为什么串在一起的插入符号相当于波浪号表达式,因为它们指的是第一个父代的(第一个父代的)第一个父代,等等,严格基于连接插入符号上的数字或波浪号后面的数字(无论哪种方式,它们都意味着相同的事情),即保持第一个父代并向上x代。
HEAD~2(或HEAD^^)指的是在层次结构中当前提交(HEAD)上/上两级祖先的提交,这意味着HEAD的祖父级提交。
HEAD^2, on the other hand, refers NOT to the first parent's second parent's commit, but simply to the second parent's commit. That is because the caret means the parent of the commit, and the number following signifies which/what parent commit is referred to (the first parent, in the case when the caret is not followed by a number [because it is shorthand for the number being 1, meaning the first parent]). Unlike the caret, the number that follows afterwards does not imply another level of hierarchy upwards, but rather it implies how many levels sideways, into the hierarchy, one needs to go find the correct parent (commit). Unlike the number in a tilde expression, it is only one parent up in the hierarchy, regardless of the number (immediately) proceeding the caret. Instead of upward, the caret's trailing number counts sideways for parents across the hierarchy [at a level of parents upwards that is equivalent to the number of consecutive carets].
所以HEAD^3等于HEAD提交的第三个父结点(不是曾祖结点,即HEAD^^^ AND HEAD~3)。
HEAD~和HEAD^之间区别的一个实际例子:
TLDR
~是你大多数时候想要的,它引用过去提交到当前分支
^引用父节点(git-merge创建第二个或更多父节点)
A~总是等于A^ A~~总是和A^^一样,以此类推 A~2并不等于A^2, 因为~2是~~的缩写 虽然^2不是任何东西的缩写,但它意味着第二个父元素