我有一个有趣的事情发生在使用git,想知道是否有人可以解释给我,所以我可以更好地理解。
合并多个分支(A,B)时,
git merge A B
以非快进方式失败,而
git merge B A
运作良好。为什么会这样?
答案 0 :(得分:46)
我们假设A是当前分支的严格direct child。然后假设B是严格的直接孩子的。
章鱼合并,processes heads given as arguments from left to right,相对于树递增,但独立于索引成功而没有冲突,如果它试图申请B然后是A,但如果进行转换则会遇到冲突。
根据git-merge
手册, MERGE STRATEGIES 部分:
octopus
This resolves cases with more than two heads, but refuses to do a
complex merge that needs manual resolution.
例如:
~ $ git init testdir && cd testdir && echo "This is C" > myfile
Initialized empty Git repository in /home/huitseeker/testdir/.git/
~/testdir $ git add myfile && git commit -m "C"
[master (root-commit) f0c8c82] C
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 myfile
~/testdir(master) $ git checkout -b "A" && echo "This is A1" > myfile
Switched to a new branch 'A'
~/testdir(A) $ git commit -m "A1" myfile
[A ac5b51c] A1
1 files changed, 1 insertions(+), 1 deletions(-)
~/testdir(A) $ git checkout -b "B" && echo "This is B1" >> myfile
Switched to a new branch 'B'
~/testdir(B) $ git commit -m "B1" myfile
[B 5bc838c] B1
1 files changed, 1 insertions(+), 0 deletions(-)
~/testdir(B) $ git checkout master
Switched to branch 'master'
~/testdir(master) $ git merge B A
Fast-forwarding to: B
Already up-to-date with A
Merge made by octopus.
myfile | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
~/testdir(master) $ git reset --hard HEAD^^^
HEAD is now at f0c8c82 C
~/testdir(master) $ git merge A B
Fast-forwarding to: A
Fast-forwarding to: B
error: Entry 'myfile' would be overwritten by merge. Cannot merge.
Merge with strategy octopus failed.
~/testdir(master) $ cat myfile
This is A1
实际上,当快速转发到A时,虽然树有,但是master的标签还没有被推进。
~/testdir(master) $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: myfile
#
如果查看章鱼合并的代码,我会手动执行此操作(请查看上面的哈希):
~/testdir(master) $ git reset --hard f0c8c82
HEAD is now at f0c8c82 C
~/testdir(master) $ git read-tree -u -m f0c8c82 ac5b51c
~/testdir(master) $ git read-tree -u -m f0c8c82 5bc838c
error: Entry 'myfile' would be overwritten by merge. Cannot merge.
在另一个方向(merge B A
),现在,如果再看一下merge-octopus的代码,它会尝试检测我们尝试添加的分支已经在树中(第二个{{1 } case
循环)。实际上,在A的合并中,它看到ac5b51c(a.k.a. A的头部)是A和B的共同祖先,并且在不执行第二个for
的情况下中止。
这个行为与git的新版本一致:虽然我已经指出v.1.3.1,但我的版本仍然会发生这种情况。
read-tree
tl; dr:您希望章鱼合并分支能够触及不同的文件