如何让 WinMerge 成为我的 git mergetool?

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

How can I make WinMerge my git mergetool?

gitwinmergedifftoolmergetool

提问by Wei Jin

I'm trying to integrate WinMerge with Git as I've seen others done before on Windows 7 Ultimate.

我正在尝试将 WinMerge 与 Git 集成,就像我之前在 Windows 7 Ultimate 上看到的那样。

I've followed the following steps, but an error continues to show up when I do a git mergetool which defaults to vimdiff.

我已经按照以下步骤操作,但是当我执行默认为 vimdiff 的 git mergetool 时,错误继续出现。

Created a file called winmerge.sh in the root directory of git: C/Program Files (x86)/Git/ with: WinMergeU is the correct location.

在 git 的根目录下创建了一个名为 winmerge.sh 的文件:C/Program Files (x86)/Git/,其中:WinMergeU 是正确的位置。

#!/bin/sh
echo Launching WinMergeU.exe:  
"C:/Program Files (x86)/WinMerge/WinMergeU.exe" 
git /e /u /dl "Base" /dr "Mine" "" ""

and used the following commands.

并使用了以下命令。

git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
git config --global difftool.prompt false

The error shows up as:

错误显示为:

git config option merge.tool set to unknown tool: winmerge

回答by eis

You are talking about merge tool, yet you (and some other people with answers) are configuring it as a diff tool.

您在谈论合并工具,但您(以及其他一些有答案的人)正在将其配置为diff 工具

To configure a merge tool, you'd need to use merge.tooland mergetoolconfigurations instead of diff.tooland difftool, like this:

要配置合并工具,您需要使用merge.toolandmergetool配置而不是diff.tooland difftool,如下所示:

git config --global merge.tool winmerge
git config --replace --global mergetool.winmerge.cmd "\"C:\Program Files (x86)\WinMerge\WinMergeU.exe\" -e -u -dl \"Base\" -dr \"Mine\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""
git config --global mergetool.prompt false

And then you can use

然后你可以使用

git mergetool

which will open you the two files to edit.

这将打开您要编辑的两个文件。

Kudos for @dvdvck mentioning in the comments that in command line parametersyou can specify a third file for the result file for winmerge (outputpath parameter).

感谢@dvdvck 在评论中提到,在命令行参数中,您可以为 winmerge(输出路径参数)的结果文件指定第三个文件。

For completeness, I'll mention that there is also this gistaimed at full configuration of winmerge for both as diff and merge tool.

为了完整起见,我会提到还有这个要点旨在完整配置 winmerge 作为差异和合并工具。

回答by CapinWinky

If you decide to use SourceTree (or for any Google searchers with SourceTree), you can use WinMerge for the Merge Tool by setting the Merge Tool to custom, pointing Diff Command to WinMergeU.exe, typically:

如果您决定使用 SourceTree(或任何带有 SourceTree 的 Google 搜索者),您可以通过将合并工具设置为自定义,将 Diff 命令指向 WinMergeU.exe,将 WinMerge 用于合并工具,通常:

C:\Program Files (x86)\WinMerge\WinMergeU.exe

In Arguments use:

在参数中使用:

-e -u -dl "Mine" -wr -dr "Theirs" $LOCAL $REMOTE $MERGED

That will cause the left side (labeled "Mine") to be editable and it will be the output file when you save in WinMerge. The right side (labeled "Theirs") will be read only (that's the -wr argument), this is needed because WinMerge outputs all saved files to the $MERGED file, so if both sides were edited, it would output the left side then overwrite that with the right side; best to avoid that kind of confusion.

这将导致左侧(标记为“我的”)可编辑,并且当您在 WinMerge 中保存时它将成为输出文件。右侧(标记为“他们的”)将是只读的(即 -wr 参数),这是必需的,因为 WinMerge 将所有保存的文件输出到 $MERGED 文件,因此如果两侧都被编辑,它将输出左侧然后用右侧覆盖它;最好避免这种混乱。

If you leave the backup file option turned on, WinMerge will generate a .bak file. The contents of this file will either be the original left side file, or the second to last output file if you saved multiple times. You can either turn this off, or add *.bak to your .gitignore file.

如果您将备份文件选项保持打开状态,WinMerge 将生成一个 .bak 文件。如果多次保存,该文件的内容将是原始左侧文件,或者是倒数第二个输出文件。您可以关闭此功能,也可以将 *.bak 添加到您的 .gitignore 文件中。

Git itself will create a *.orig conflict file AFTER the conflict is resolved, just in case you botched it. Again, you can add *.orig to your .gitignore file or turn off this feature. Unfortunately, SourceTree does not have a GUI option for this, so fire up your git bash or, if you chose the right PATH option during installation, the Windows Command prompt and do this:

Git 本身会在冲突解决后创建一个 *.orig 冲突文件,以防万一你搞砸了。同样,您可以将 *.orig 添加到您的 .gitignore 文件或关闭此功能。不幸的是,SourceTree 没有为此提供 GUI 选项,因此启动您的 git bash,或者,如果您在安装过程中选择了正确的 PATH 选项,则在 Windows 命令提示符下执行以下操作:

git config --global mergetool.keepBackup false

That will stop Git creating the *.orig files. You can also directly edit the config file by locating the .gitconfig file in the root of your user directory. If you know how to use VIM, you can edit the whole thing with this command:

这将停止 Git 创建 *.orig 文件。您还可以通过在用户目录的根目录中找到 .gitconfig 文件来直接编辑配置文件。如果您知道如何使用 VIM,则可以使用以下命令编辑整个内容:

git config --global --edit

回答by VonC

Git 2.5+ (Q2 2015) will include Winmerge as a known git mergetool!

Git 2.5+(2015 年第二季度)将包含 Winmerge 作为已知的git mergetool!

If Winmerge is in your %PATH%, a git config merge.tool winmergeis all you need to do!
(It works for diff tool too: git config diff.tool winmerge)

如果是的WinMerge你%PATH%,一个git config merge.tool winmerge是所有你需要做的!
(它适用于比较工具也:git config diff.tool winmerge

See commit 3e4f237by David Aguilar (davvid), 20 May 2015.
(Merged by Junio C Hamano -- gitster--in commit 324a9f4, 01 Jun 2015)
Helped-by: Philip Oakley (PhilipOakley), Johannes Schindelin (dscho), Sebastian Schuberth (sschuberth), SZEDER Gábor (szeder)

提交3e4f237大卫·阿吉拉尔(davvid,5月20日到2015年
(由合并JUNIOÇ滨野- gitster-提交324a9f4,2015年6月1日)
的帮助下,通过:菲利普·奥克利(PhilipOakley约翰内斯Schindelin( )dscho塞巴斯蒂安Schuberth( )sschuberthSZEDER Gábor ( szeder)

All the config is now done for you directly in Git itself, with mergetools/winmerge:

现在所有的配置都直接在 Git 中为你完成,使用mergetools/winmerge

  • diff command: "$merge_tool_path" -u -e "$LOCAL" "$REMOTE"
  • merge command: "$merge_tool_path" -u -e -dl Local -dr Remote "$LOCAL" "$REMOTE" "$MERGED"
  • 差异命令: "$merge_tool_path" -u -e "$LOCAL" "$REMOTE"
  • 合并命令: "$merge_tool_path" -u -e -dl Local -dr Remote "$LOCAL" "$REMOTE" "$MERGED"

mergetools: add winmerge as a builtin tool

Add a winmerge scriptlet with the commands described in this thread, so that users can use winmerge without needing to perform any additional configuration.

mergetools: 添加 winmerge 作为内置工具

使用此线程中描述的命令添加 winmerge 脚本,以便用户可以使用 winmerge,而无需执行任何其他配置。

回答by ellotheth

Here's mine (in %userprofile%\.gitconfig, or ~/.gitconfigon *nix), no wrapper (Win 7 Home Pro):

这是我的(在*nix 中%userprofile%\.gitconfig~/.gitconfig在 *nix 上),没有包装器(Win 7 Home Pro):

[diff]
    tool = winmerge
[difftool "winmerge"]
    cmd = c:/path/to/winmergeu.exe -e -u -x -wl -wr -dl "base" -dr "mine" \"$LOCAL\" \"$REMOTE\"

回答by gman

This is easier to do and is what worked for me:

这更容易做到,并且对我有用:

git config --global diff.tool winmerge

git config --replace --global difftool.winmerge.cmd "\"C:\path to winmerge\WinMergeU.exe\" -e -u -dl \"Base\" -dr \"Mine\" $LOCAL $REMOTE"

git config --global difftool.prompt false

回答by Zin Min

To do WinMerge as compare and merge tool for Visual Studio 2017 Git Plugin:

要将 WinMerge 作为 Visual Studio 2017 Git 插件的比较和合并工具:

From windows command prompt: type >> git config --global --edit which will open the .getconfig file to edit.

在 Windows 命令提示符下:键入 >> git config --global --edit 这将打开 .getconfig 文件进行编辑。

Please update with below command:

请使用以下命令更新:

[mergetool]
   prompt = false
   keepBackup = false
   keepTemporaries = false
[merge]
   tool = winmerge
   [mergetool "winmerge"]
   name = WinMerge
   trustExitCode = true
   cmd = \"C:\Program Files (x86)\WinMerge\WinMergeU.exe\" -e -u -dl \"Base\" -dr \"Mine\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
[diff]
   tool = winmerge
   [difftool "winmerge"]
   name = WinMerge
   trustExitCode = true
   cmd = \"C:\Program Files (x86)\WinMerge\WinMergeU.exe\" -u -e $LOCAL $REMOTE

回答by Thach Mai

Your path is incorrect, it should be "/c/Program Files (x86)/WinMerge/WinMergeU.exe".

你的路径不正确,应该是"/c/Program Files (x86)/WinMerge/WinMergeU.exe".

You're running in a shell script environment, not native windows command prompt.

您在 shell 脚本环境中运行,而不是本机 Windows 命令提示符。

回答by trank

After installing TortoiseGit and WinMerge 2.16 (this version supports 3-way merging), I found how WinMerge integrate itself to TortoiseGit:

在安装了 TortoiseGit 和 WinMerge 2.16(这个版本支持三路合并)之后,我发现了 WinMerge 是如何集成到 TortoiseGit 的:

D:\Program Files\WinMerge\WinMergeU.exe /e /ub /fm /wl /wr /dl %tname /dm %bname /dr %yname  %theirs %base %mine /o %merged /am

So I edited the .gitconfigfile and change some vars to make it work for me (I added WinMerge dir to the system path):

所以我编辑了.gitconfig文件并更改了一些变量以使其对我有用(我将 WinMerge 目录添加到系统路径中):

[mergetool "winmerge"]
    cmd = WinMergeU -e -ub -fm -wl -wr $LOCAL $BASE $REMOTE -o $MERGED -am

回答by Isaac Brown

Entering the settings via the command line has been covered by other answers. The .gitconfigfile for full 3-way merging with WinMerge could be configured like this (this example is from Windows):

其他答案已涵盖通过命令行输入设置。.gitconfig与 WinMerge 完全 3 路合并的文件可以这样配置(这个例子来自 Windows):

[merge]
    tool = WinMerge

[mergetool "WinMerge"]
    cmd = \"C:\Program Files\WinMerge\WinMergeU.exe\" -e -u -dl \"Local\" -dm \"Base\" -dr \"Remote\" \"$LOCAL\" \"$BASE\" \"$REMOTE\" -o \"$MERGED\"
    trustExitCode = true
    keepBackup = false

[diff]
    tool = WinMerge

[difftool "WinMerge"]
    cmd = \"C:\Program Files\WinMerge\WinMergeU.exe\" -e -u -dl \"Old $BASE\" -dr \"New $BASE\" \"$LOCAL\" \"$REMOTE\"
    trustExitCode = true

Flag information:

旗帜信息:

  • /e - Allows WinMerge to be closed via a single press of the 'esc' key.
  • /u - Prevents WinMerge from logging the files in the recently used list.
  • /dl, /dm, and /dl - Descriptions for Left, Middle, and Right panes.
  • /o - The output file. Saving ANYpane will output that pane's contents to the output file.
  • /e - 允许通过单击“esc”键关闭 WinMerge。
  • /u - 防止 WinMerge 在最近使用的列表中记录文件。
  • /dl、/dm 和 /dl - 左窗格、中窗格和右窗格的说明。
  • /o - 输出文件。保存任何窗格都会将该窗格的内容输出到输出文件。

trustExitCode = truetells git to accept the output without further prompt.

trustExitCode = true告诉 git 在没有进一步提示的情况下接受输出。

keepBackup = falsewill automatically delete the automatically generated *.origfiles.

keepBackup = false会自动删除自动生成的*.orig文件。

Note: The $BASE and $MERGE variables when using difftool both simply contain the filename.

注意:使用 difftool 时的 $BASE 和 $MERGE 变量都只包含文件名。

回答by Daz

I originally upvoted @CapinWinky's answer for using WinMerge from SourceTree. That answer is still valid, but WinMerge now also supports a three-way merge which needs additional parameters.

我最初赞成@CapinWinky 对使用 SourceTree 中的 WinMerge 的回答。该答案仍然有效,但 WinMerge 现在还支持需要附加参数的三向合并。

Within the SourceTree Options dialog I chose "Custom" as the Merge Tool, entered the command as:

在 SourceTree 选项对话框中,我选择“自定义”作为合并工具,输入命令为:

C:\Program Files\WinMerge\WinMergeU.exe

and the Arguments as:

和参数为:

-u -fm -wl -wr -dl Remote_RO -dm Local_Merged -dr Base_RO $REMOTE $LOCAL $BASE -o $MERGED

It doesn't seem to be necessary to enclose the remote/local/base parameters in quotes to cater for paths with spaces in them any more.

似乎没有必要将 remote/local/base 参数括在引号中来满足其中包含空格的路径。

This configuration is for using the middle pane as the merged output file.

此配置用于将中间窗格用作合并的输出文件。

Command line reference

命令行参考

I had the problem that when trying to merge, WinMerge wouldn't open. Sourcetree starts Git to open your tool of choice. When using Sourcetree's custom option, your git config file (e.g. C:\Users\Me\.gitconfig) is modified to add a new Merge tool called "sourcetree" and the Git command includes --tool=sourcetree. At some point the .gitconfig file hadn't been updated correctly and I had two 'cmd' lines in that section, one being broken:

我遇到的问题是,在尝试合并时,WinMerge 无法打开。Sourcetree 启动 Git 以打开您选择的工具。当使用 Sourcetree 的自定义选项时,您的 git 配置文件(例如 C:\Users\Me\.gitconfig)被修改以添加一个名为“sourcetree”的新合并工具,并且 Git 命令包括--tool=sourcetree. 在某些时候,.gitconfig 文件没有正确更新,我在该部分有两个“cmd”行,一个被破坏了:

[mergetool "sourcetree"]
    cmd = 'C:/Program Files/WinMerge/WinMergeU.exe' -u -fm -wl -wr -dl Remote_RO -dm Local_Merged -dr Base_RO $REMOTE $LOCAL $BASE -o $MERGED
    trustExitCode = true
    cmd = 'C:/Program '

Manually fixing up the .gitconfig file sorted the problem.

手动修复 .gitconfig 文件可以解决问题。