我只是无法理解这种情况是如何造成的。我试图使用测试存储库在分支之间重新创建这种纵横交错的合并情况,但我无法复制它。在所有情况下,我总是以 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
现在修改最后一次提交(有关更多详细信息,请参阅git commit --amend 究竟是如何工作的?):
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
你去吧:历史现在包含纵横交错的合并。
这是常见情况还是错误情况?
如上所述,可能会出现这种情况。这可能是不可取的,但不应将其视为“错误状态”。