如何查看 Git 合并冲突的“三向差异”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18131515/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:44:21  来源:igfitidea点击:

How can I see a "three way diff" for a Git merge conflict?

git

提问by Nathan Long

Suppose I'm on Git branch masterand I git merge featurebranch. There is a conflict in foo.html.

假设我在 Git 分支上master,我git merge featurebranch. 中存在冲突foo.html

When I open foo.html, I see, in the area of the conflict, what masterhas and what featurebranchhas. But I can't really tell what changewas made on masterthat conflicted with featurebranch; I only know what master has now.

当我打开 时foo.html,我会看到在冲突区域中的内容master和内容featurebranch。但我真的不知道在与;冲突的地方做了什么改变;我只知道主有现在masterfeaturebranch

I'd like to see the diffthat each one applied.

我想看看每个应用的差异

Or, to get the same information, I could see:

或者,为了获得相同的信息,我可以看到:

  • The version masterhas now
  • The version featurebranchhas now
  • The version their common ancestorhad
  • 现在版本master
  • 现在版本featurebranch
  • 该版本他们共同的祖先

How can I see this?

我怎么能看到这个?

回答by RazerM

From git-merge(1),

git-merge(1)

An alternative style can be used by setting the "merge.conflictstyle" configuration variable to "diff3".

In addition to the <<<<<<<, =======, and >>>>>>>markers, it uses another |||||||marker that is followed by the original text. ... You can sometimes come up with a better resolution by viewing the original.

可以通过将“merge.conflictstyle”配置变量设置为“diff3”来使用替代样式。

除了<<<<<<<=======>>>>>>>标志,它使用另一个|||||||后跟原文标记。...您有时可以通过查看原件获得更好的分辨率。

This can be enabled using

这可以使用启用

git config --global merge.conflictstyle diff3

or right in ~/.gitconfigfile

或在~/.gitconfig文件中

[merge]
  conflictstyle = diff3

回答by Nathan Long

A lot of GUI diff/merge tools have a 3 or 4 way merge view. I highly recommend Beyond Comparefor resolving merge conflicts. Another (okay) tool is DiffMerge.

许多 GUI 差异/合并工具都有 3 或 4 路合并视图。我强烈推荐Beyond Compare来解决合并冲突。另一个(好的)工具是DiffMerge

You can set up a custom mergetool to use with the git mergetoolcommand. This is my .gitconfigconfiguration for Beyond Compare (Pro edition) and DiffMerge on my Windows machine using msysgit:

您可以设置自定义合并工具以与git mergetool命令一起使用。这是我.gitconfig在 Windows 机器上使用 msysgit 对 Beyond Compare(专业版)和 DiffMerge 的配置:

[merge]
    tool = bc3
[diff]
    tool = bc3
[difftool "dm"]
    cmd = C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe \"$LOCAL\" \"$REMOTE\"
[difftool "bc3"]
    cmd = "\"c:/program files (x86)/beyond compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\""
[mergetool "bc3"]
    cmd = "\"c:/program files (x86)/beyond compare 3/bcomp.exe\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\""

You can read more about various diff/merge tool configurations in the official Linux Kernel Git documentation for git config.

您可以在官方 Linux 内核 Git 文档中git config阅读有关各种差异/合并工具配置的更多信息。