git - Git 中如何产生交叉 merge ?

标签 git git-merge least-common-ancestor

我一直在浏览“git merge-base”手册页,但我无法理解多个 merge 基础是如何发展的。具体来说,我对手册页中的以下插图很感兴趣:

When the history involves criss-cross merges, there can be more than one best common
ancestor for two commits. For example, with this topology:
  ---1---o---A
      \ /
       X
      / \
  ---2---o---o---B
both 1 and 2 are merge-bases of A and B. Neither one is better than the other (both
are best merge bases). When the --all option is not given, it is unspecified which
best one is output.

我就是不明白怎么会出现这种情况。我试图使用测试存储库在分支之间重新创建这种交叉 merge 情况,但我无法复制它。在所有情况下,我总是以 A 和 B 都指向的 1 个 merge 提交结束(而不是 A 和 B 指向独立的 merge 提交,如图所示)。

谁能说明这种情况是如何发生的? 这是常见情况还是错误情况?

最佳答案

I just can't understand how such a situation could be created. I have attempted to recreate this criss-cross merge situation between branches using a test repository but I cannot replicate it. In all cases, I always wind up with 1 merge commit to which both A and B point to (instead of A and B pointing to independent merge commits as the diagram illustrates).

Can anyone illustrate how this situation can arise?

交叉 merge 可以以不同的方式出现。举个例子,当你有两个分支引用指向同一个 merge 提交(其中一个分支被 checkout )并且你运行 git commit --amend 时。以下玩具示例就是这样做的:

# set things up
cd ~/Desktop
mkdir crisscross
cd crisscross
git init

# make an initial commit
printf "bar\n" > README.md
git add README.md
git commit -m "add README"

# add a line at the top of the file and commit
sed -i '' '1s/^/foo\n/' README.md
git commit -am "add foo line"

# create and checkout a branch pointing at the initial commit
git checkout -b other master^

# add a line at the bottom of the file and commit
printf "baz\n" >> README.md
git commit -am "add baz line"

# do a merge and make both branches point to this merge commit
git merge master
git checkout master
git merge other

在这个阶段,git log --graph --oneline --decorate --all的输出是

*   30b9175 (HEAD, master, other) Merge branch 'master' into other
|\  
| * f318ead add foo line
* | 31875d9 add baz line
|/  
* 8b313a0 add README

现在修改最后一次提交(更多细节参见How does git commit --amend work, exactly?):

git commit --amend

之后,git log --graph --oneline --decorate --all 的输出将是

*   69bbcbe (HEAD, master) Amended merge commit
|\  
| | *   30b9175 (other) Merge branch 'master' into other
| | |\  
| |/ /  
|/| /   
| |/    
| * f318ead add foo line
* | 31875d9 add baz line
|/  
* 8b313a0 add README

好了:历史现在包含交叉 merge 。

Is this a common situation or an error situation?

如上所示,可能会出现这种情况。这可能是不可取的,但不应将其视为“错误状态”。

关于git - Git 中如何产生交叉 merge ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370185/

相关文章:

git - 如何将 A 和 K 之间的提交压缩为一个提交 Z

git - 为什么我在使用 git 时出错,但在 rebar3 中使用 hex deps 时却没有

git - 说服 git blame 一个分支比另一个分支与历史更相关

git - Git可以将一个裸仓库克隆到另一个裸仓库吗

node.js - 一个 git 存储库中的多个模块;只需要向 Heroku 部署一个模块

git - 在不覆盖数据的情况下进行 git checkout

git - 解决添加和删除的冲突 Git

algorithm - 最低共同祖先算法的实际应用是什么?

algorithm - 如何表示非二叉树以及如何对该树进行 LCA?