如何确定哪个 parent 是 Git 中合并提交的第一个

How to determine which parent is the first one for a merge commit in Git

我正在阅读 git 中使用 ~^ 运算符的区别,我遇到了这个问题 What's the difference between HEAD^ and HEAD~ in Git?

谷歌搜索后我无法在网上找到很好的解释的一件事是 git 如何区分合并提交的第一个 parent 和第二个?

有经验法则吗?

以这个例子为例,其中 feature 分支被合并 develop 分支,创建合并提交 G.

develop feature/foo
     A   D
     |   |
     B   E
     |   |
     C   F
      \ /
       G <- develop(HEAD)

G 的第一个 parent 是哪个? C 还是 F?为什么是第一个parent?

注意:这不是请求 git 命令确定第一个或第二个 parent。我知道可以使用 git show G^1git show G^2 来实现。我看到 CG 的第一个 parent,F 是第二个 parent。但是,我不明白为什么会这样。是否像合并提交所在的分支决定了第一个 parent?

因此,没有父提交本身。但是您可以根据 运行 git merge

的 b运行ch 来判断哪个是父级

Is it like the branch on which the merge commit is made determines the first parent?

是的。

参考 Pro Git 的 Git-Tools-Revision-Selection :

You can also specify a number after the ^ to identify which parent you want; for example, d921970^2 means “the second parent of d921970.” This syntax is useful only for merge commits, which have more than one parent — the first parent of a merge commit is from the branch you were on when you merged (frequently master), while the second parent of a merge commit is from the branch that was merged (say, topic):

git show G^1git show G^2git log -1 --pretty=%P Ggit rev-parse G^@等命令可以证明它与描述的相同。此外,使用git cat-file -p G查看commit对象的内容,可以看到父类记录在订单中。

第一个 parent 通常 您执行合并的分支的 HEAD 提交。

git commit 使用 HEAD 作为第一个 parent 提交(如果没有冲突,git merge 会为你做提交,否则你自己做. 无论哪种方式,结果都是一样的)。所有方便的命令都遵循此约定。您可以使用 git commit-tree 自行构建任意提交,并出于任何原因指定您喜欢的任何 parent。