如何在Git中出现纵横交错的合并?

MAC*_*ACS 6 git git-merge least-common-ancestor

我一直在浏览"git merge-base",手册页,我无法理解多个合并库是如何发展的.具体来说,我挂在手册页中的下图中:

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.
Run Code Online (Sandbox Code Playgroud)

我无法理解如何创造这种情况.我试图使用测试存储库在分支之间重新创建这种纵横交错的合并情况,但我无法复制它.在所有情况下,我总是最终得到A和B指向的1个合并提交(而不是A和B指向独立的合并提交,如图所示).

谁能说明这种情况会怎样?这是常见情况还是错误情况?

jub*_*0bs 8

我无法理解如何创造这种情况.我试图使用测试存储库在分支之间重新创建这种纵横交错的合并情况,但我无法复制它.在所有情况下,我总是最终得到A和B指向的1个合并提交(而不是A和B指向独立的合并提交,如图所示).

谁能说明这种情况会怎样?

纵横交错可能以不同的方式出现.在你想到的例子中,有两个分支引用指向同一个合并提交(其中一个分支被检出)并运行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
Run Code Online (Sandbox Code Playgroud)

在这个阶段,输出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
Run Code Online (Sandbox Code Playgroud)

现在修改最后一次提交(请参阅git commit --amend如何正常工作?更多细节):

git commit --amend
Run Code Online (Sandbox Code Playgroud)

之后,输出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
Run Code Online (Sandbox Code Playgroud)

你去了:历史现在包含纵横交错合并.

这是常见情况还是错误情况?

如上所述,可能出现这种情况.这可能是不合需要的,但不应将其视为"错误状态".