我如何在我的 Git 存储库中解决合并冲突?


当前回答

查看 Stack Overflow 问题中的答案 堕胎在 Git 中的合并,特别是 Charles Bailey 的答案,显示如何查看不同版本的文件有问题,例如,

# Common base version of the file.
git show :1:some_file.cpp

# 'Ours' version of the file.
git show :2:some_file.cpp

# 'Theirs' version of the file.
git show :3:some_file.cpp

其他回答

试试:

git mergetool

它打开一个GUI,你通过每个冲突,你得到选择如何合并。有时它需要一些手编辑后,但通常它是足够的本身。


根据Josh Glover的评论:

[此命令] 不必打开 GUI 除非你安装一个. 运行 git mergetool 为我导致 vimdiff 被使用. 您可以安装下列工具之一来使用它: meld, opendiff, kdiff3, tkdiff, xxdiff, tortoisemerge, gvimdiff, diffuse, ecmerge, p4merge, araxis, vimdiff, emerge。


下面是使用vimdiff解决合并冲突的样本程序,基于此链接。

我要我的或他们的版本完整,或者想审查个别的变化,并决定每一个。

完全接受我的或他们的版本:

接受我的版本(本地,我们的):

git checkout --ours -- <filename>
git add <filename>              # Marks conflict as resolved
git commit -m "merged bla bla"  # An "empty" commit

接受其版本(远程,自己的):

git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"

如果你想做所有冲突文件运行:

git merge --strategy-option ours

git merge --strategy-option theirs

查看所有变更并单独接受它们

git mergetool 评论 改变并接受每个版本. git 添加 <filename> git commit -m "merged bla bla"

默认的 mergetool 在命令线上工作. 如何使用命令线 mergetool 应该是一个单独的问题。

您也可以为此安装视觉工具,例如通讯和运行

git mergetool -t meld

它将打开本地版本(ours),“base”或“合并”版本(合并的当前结果)和远程版本(天花板)。在完成时保存合并版本,运行 git mergetool -t 再次登记,直到您获得“不需要合并文件”,然后转到步骤3和4。

查看 Stack Overflow 问题中的答案 堕胎在 Git 中的合并,特别是 Charles Bailey 的答案,显示如何查看不同版本的文件有问题,例如,

# Common base version of the file.
git show :1:some_file.cpp

# 'Ours' version of the file.
git show :2:some_file.cpp

# 'Theirs' version of the file.
git show :3:some_file.cpp

我喜欢使用WinMerge(免费工具),它既是整个目录树的比较/混合,而且是整个目录树的个别文件(s)比较/混合。

解决的步骤可以是:

也就是说,没有魔法的方式来解决 Git 合并的冲突,除了手动观察每个人对相同的源文件(s)做了什么。

这就是我所想的。

注意: WinMerge 创建.bak 文件.. 并且您不希望它们复制到源控制 AzOps, TFS 等,所以如果您确信您已经完成了正确的编辑,删除.bak 文件。

对于使用 Visual Studio 的用户(Visual Studio 2015 在我的情况下)

关闭您的项目在Visual Studio. 特别是在大型项目,Visual Studio倾向于在使用UI合并时脱离。 立即在命令中进行合并. git checkout target_branch git merge source_branch 然后在Visual Studio中打开项目,然后转到Team Explorer → Branch。