git - 如何在具有共同祖先的 2 个分支之间 merge 冲突?

标签 git merge version-control

我们最近在尝试 merge 两个分支时使用 git 遇到了一个非常奇怪的问题。情况归结为以下几点:

  1. branch A 是我们想要 merge 回主干 B 的一些长期分支,它有一些其他提交
  2. 我们想将 B merge 到 A 中,然后将 A merge 回 B
  3. 在执行 git checkout A 时; git merge B 我们遇到了与 A 引入的更改无关的 merge 冲突:这些冲突位于一堆文件中,据称这些文件未被 A 触及,但是在 B
  4. 中更改
  5. 使用 git annotategit log 来跟踪其中一个冲突文件的祖先,我们有效地注意到冲突位置具有共同的提交祖先 1234: 一行在A中被注释为1234,在B中被注释为5678,但是git annotate file1 5678^ 显示冲突行的 1234

我不明白这是怎么可能的,也找不到任何关于类似问题的线索。

最佳答案

在分支 B 的共同祖先之后,应该在分支 A 上更改冲突文件。您可以再次通过 git annotategit 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/

相关文章:

git - VSCode 源代码控制不工作,而 VSCode 终端 Git 工作

xcode - 如何在 Xcode 中删除未使用的源代码管理主机?

r - 合并具有所有组合的两个数据框

svn - 颠覆: more files than expected when merging a branch to trunk

image - NSImage-drawAtPoint : Draws Too Small

git - 我可以将本地 Git 存储库安全地移动到硬盘驱动器中的其他位置吗?

cvs - 获取最后一条消息或最后一次 CVS 提交的差异

git - 为什么我不能 checkout 另一个 git 分支?

git - 如何将某些文件推送到 Git 中的 origin/master?

父文件夹中的 Git 子模块