我们最近在尝试 merge 两个分支时使用 git 遇到了一个非常奇怪的问题。情况归结为以下几点:
- branch
A
是我们想要 merge 回主干B
的一些长期分支,它有一些其他提交 - 我们想将
B
merge 到A
中,然后将A
merge 回B
- 在执行
git checkout A 时; git merge B
我们遇到了与A
引入的更改无关的 merge 冲突:这些冲突位于一堆文件中,据称这些文件未被A
触及,但是在B
中更改
- 使用
git annotate
和git log
来跟踪其中一个冲突文件的祖先,我们有效地注意到冲突位置具有共同的提交祖先1234
: 一行在A
中被注释为1234
,在B
中被注释为5678
,但是git annotate file1 5678^
显示冲突行的1234
。
我不明白这是怎么可能的,也找不到任何关于类似问题的线索。
最佳答案
在分支 B
的共同祖先之后,应该在分支 A
上更改冲突文件。您可以再次通过 git annotate
和 git log
仔细检查。
1。分别列出A分支和B分支修改冲突文件的commit:
git annotate filename A
git annotate filename B
注意git annotate
中的提交列表是从旧的(顶部)到新的(底部)排序的。
假设命令的输出如下:
$ git annotate filename A
commit A1
commit A2
commit common
commit A3
commit A4
$ git annotate filename B
commit B1
commit B2
commit B3
commit common
commit B4
这意味着,在提交祖先 commit common
之后,冲突文件在分支 中的提交
;在分支 A3
和提交 A4
中更改一个B
中,冲突文件是之后在 commit B4
中的更改。
2。将 git 日志显示为更改所有分支中的冲突文件的提交的图表
并且您可以使用以下命令通过图表显示更改冲突文件的提交:
git log --oneline --decorate --graph --all -- filename
然后图形将如下所示:
* commit A4 (branch A)
* commit A3
| * commit B4 (branch B)
* | commit Common
| \
* | commit A2
* | commit A1
| * commit B3
| * commit B2
| * commit B1
| |
… …
关于git - 如何在具有共同祖先的 2 个分支之间 merge 冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429347/