git Git章鱼合并多个分支的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6520905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git octopus merge order of multiple branches
提问by James Maroney
I had an interesting thing happen using git, wondering if anyone could explain it to me so I can understand better.
我在使用 git 时发生了一件有趣的事情,想知道是否有人可以向我解释它以便我更好地理解。
When doing a merge of multiple branches (A,B),
合并多个分支(A,B)时,
git merge A B
fails as non-fast-forward, while
因非快进而失败,而
git merge B A
worked well. Why would that be?
工作得很好。为什么会这样?
回答by Francois G
Let's assume that A is a strict, direct childof the current branch. Then assume that B is a strict, direct child of A.
让我们假设 A 是当前分支的严格直接子节点。然后假设 B 是 A 的严格直接子节点。
The octopus merge, which processes heads given as arguments from left to right, incrementally with respect to the tree, but independently with respect to the indexsucceeds without conflict if it tries to apply B and then A, but encounters a conflict if it does the convert.
章鱼合并,这从左至右处理作为参数头,增量相对于树,但独立相对于该指数成功,没有冲突,如果它试图申请B,然后A,但如果它确实遇到冲突转变。
As per the git-merge
manual, section MERGE STRATEGIES:
根据git-merge
手册,部分合并策略:
octopus
This resolves cases with more than two heads, but refuses to do a
complex merge that needs manual resolution.
For instance:
例如:
~ $ 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
Indeed, when fast-forwarding to A, the label of master has not been pushed forward, though the tree has.
事实上,当快进到 A 时,master 的标签并没有被推进,尽管树已经被推进了。
~/testdir(master) $ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: myfile
#
If, looking at the code of what the octopus merge does, I perform this manually (look above for hashes):
如果查看章鱼合并的代码,我手动执行此操作(查看上面的哈希值):
~/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.
In the other direction (merge B A
), now, if you look again at the code of merge-octopus, it tries to detect the branch we are trying to add is already in the tree (second case
of the for
loop). Indeed, at the merge of A, it sees ac5b51c (a.k.a. A's head) is the common ancestor of A and B, and aborts without doing the second read-tree
.
在另一个方向上(merge B A
),现在,如果你在合并,章鱼的代码再看看,它会尝试检测,我们正在尝试添加已经在树(第二分支case
的for
循环)。实际上,在 A 的合并中,它看到 ac5b51c(又名 A 的头部)是 A 和 B 的共同祖先,并且在不执行第二个 的情况下中止read-tree
。
This behavior is consistent with the fresh version of git : though I've pointed to v.1.3.1, this is still happening with my version.
这种行为与 git 的新版本一致:虽然我已经指出了 v.1.3.1,但我的版本仍然会发生这种情况。
~/testdir(master) $ git --version
git version 1.7.5.4
tl;dr : you want your octopus merge branches to touch distinct files
tl;dr :您希望章鱼合并分支接触不同的文件