Git父母和祖先之间的逻辑区别是什么 [英] Git what is the logical difference between parent and ancestor
问题描述
我知道我可以找到许多有关HEAD^
和HEAD~
I know I can find dozens of questions about the difference between HEAD^
and HEAD~
-
~
指定ancestors
-
^
指定parents
~
specifiesancestors
^
specifiesparents
我的问题是关于parents
和ancestors
含义的区别.对我而言,父母与祖先相同.
My question is about the difference of meaning of parents
and ancestors
. for me parent is the same as ancestor.
父母与祖先之间的逻辑区别是什么?
What is the logical difference between parent and ancestor?
Git中的HEAD ^和HEAD〜有什么区别没有回答我的问题,我的问题是关于这两个术语的逻辑含义.
What's the difference between HEAD^ and HEAD~ in Git? doesn't answer my question, my question is about the logical meaning of the two terms.
推荐答案
区别是垂直和水平问题.
The difference is a bit of a vertical vs horizontal matter.
在Git中,提交是链接列表的一种,每个提交都引用一个父提交或一个多个父提交.
In Git, commits are a type of linked list, each commit has a reference to a parent commit, or to multiple parent commits.
通过~
访问父级或祖父母级:
Accessing a parent, or a grand-parent, is done with ~
:
-
~
是提交的直接父级, -
~2
(或~~
)是祖父母 - 依此类推
~
is the direct parent of a commit,~2
(or~~
) is the grand-parent- and so on
这是垂直的一面,如果您查看日志,则使用~
将使您深入了解提交.
This is the vertical side, if you look at a log, using ~
will make you go down through the commits.
现在,当您合并两个分支时,它将进行合并提交.根据定义,合并提交是将两个或多个分支合并在一起.因此,合并提交将有两个或多个父级.
Now, when you merge two branches, it makes a merge commit. A merge commit, by definition, is merging two, or more, branches together. So the merge commit will have two or more parents.
在合并提交中,如果要访问不同的父级,请使用^
(实际上看起来像是两个分支的合并).
From the merge commit, if you want to access different parents, you use ^
(it actually looks like a merge of two branches).
因此请考虑日志的这一部分:
So consider this part of log:
* 39a2f899 (HEAD)
|\
| * e2e7d241 (BRANCH)
| * caf13dc1
| * 609a9715
|/
* 663e09ff
如果要访问BRANCH
的父提交,则可以执行BRANCH~
或e2e7d241~
,这将给出caf13dc1
.现在,如果您要访问HEAD
的父级并执行HEAD~
,它将为您提供663e09ff
(分支开始之前的提交).
If you want to access the parent commit of BRANCH
, you would do BRANCH~
, or e2e7d241~
, which would give caf13dc1
. Now if you want to access the parent of HEAD
, and do HEAD~
, it will give you 663e09ff
(the commit before the branch started).
但是,如果您想访问合并分支的提交链,那就是^
在游戏中出现的位置,您可以执行HEAD^2
,说"HEAD的第二父级"(而不是父级的父级)
But if you want to access the chain of commits of the merged branch, that's where ^
comes in the game, you would do HEAD^2
, to say the "second parent of HEAD" (not the parent's parent).
您当然可以组合它们,在同一张图中,HEAD^2~
是HEAD的第二个父级(^2
)的父级(~
),caf13dc1
You can of course combine them, in the same graph, HEAD^2~
is the parent (~
) of the second parent (^2
) of HEAD, which is caf13dc1
顺便说一句,每个提交至少有1个父对象,因此commit^
始终与commit~
相同.另一方面,如果提交具有单亲(不是合并提交),则commit^2
将返回错误.
Incidentally, every commit has at least 1 parent, so commit^
is always the same as commit~
. On the other hand, if a commit has a single parent (it is not a merge commit), commit^2
will return an error.
总而言之,您可以使用^
访问合并的不同父级,并且可以使用~
访问生成的提交.
To sum up, you access different parents of a merge with ^
and you access generations of commits with ~
.
可以通过以下更复杂的合并来说明(一次合并4个分支):
It can be illustrated as follow with a more complex merge (of 4 branches merged at once):
----------> use ^ to go through the parents of the merge
|
| *-----. 6af2936d
| |\ \ \ \
| | | | | * 20d6fb23 BRANCH1
| | | | * | e589d446 BRANCH2
| | | | * | ec6088bd
| | | | |/
| | | * | 38dcecfa BRANCH3
| | | |/
| | * | 698c3daa BRANCH4
| | |/
| |/
| * 2d97958e
| |\
| | * 8989f1d3
| | * d907cb7a
| |/
| * af368002
V
use ~ to go this way (through the ancestors)
这篇关于Git父母和祖先之间的逻辑区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!