我只是不明白怎么会造成这种情况。我试图使用测试存储库在分支之间重新创建这种纵横交错的合并情况,但我无法复制它。在所有情况下,我总是以 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
现阶段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
你去吧:历史现在包含纵横交错的合并。
这是常见情况还是错误情况?
如上图所示,可能会出现这种情况。这可能是不可取的,但不应将其视为“错误状态”。