在 Windows 上为 Git 配置差异工具

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

Configuring a diff-tool for Git on Windows

gitmergediff

提问by Claus J?rgensen

Since git diffin powershell mostly results in a unreadable amount of whitespace or strange characters, I installed Perforce P4Merge, to handle merging and diff.

由于git diff在 powershell 中大多会导致无法读取的空白或奇怪字符的数量,因此我安装了 Perforce P4Merge 来处理合并和差异。

However, I can't make git diffopen the p4merge application to view the diff in. So far my .gitconfiglooks like this:

但是,我无法git diff打开 p4merge 应用程序来查看差异。到目前为止,我的.gitconfig如下所示:

[diff]
    tool = p4diff

[difftool "p4diff"]
    external = 'C:/PROGRA~1/Perforce/p4merge.exe "$LOCAL" "$REMOTE"'
    prompt = false

[merge]
    keepBackup = false
    tool = p4merge

[mergetool "p4merge"]
    cmd = "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
    path = 'C:/PROGRA~1/Perforce/p4merge.exe'
    prompt = false
    keepBackup = false
    keepTemporaries = false
    trustExitCode = false

The paths are correct. But alas, git still attempts to present the diff in Powershell, when using git diff.

路径是正确的。但遗憾的是,当使用 .git 时,git 仍会尝试在 Powershell 中显示差异git diff

So what's the trick to make this work?

那么让这项工作发挥作用的诀窍是什么?

回答by Kenny Trytek

In your command prompt, try: git config --global diff.tool p4diff

在您的命令提示符中,尝试: git config --global diff.tool p4diff

That worked for me, using a config file set up as you had described:

这对我有用,使用您描述的配置文件设置:

[diff]  
    tool = p4merge  
[difftool "p4diff"]  
    external = '"C:/Program Files/Perforce/p4merge.exe" "$LOCAL" "$REMOTE"'

Running git difftoolopened p4merge as expected.

运行git difftool按预期打开 p4merge。

Ref: How do I view 'git diff' output with a visual diff program?

参考:如何使用可视化差异程序查看“git diff”输出?

回答by Someguy

I'm on WIndows 7 using MSysGit v1.8.4.

我在使用 MSysGit v1.8.4 的 WINdows 7 上。

For me, the only solution was to add p4merge to the system path variable (reboot needed afterwards). Having done this, i conf'd it like this:

对我来说,唯一的解决方案是将 p4merge 添加到系统路径变量中(之后需要重新启动)。完成此操作后,我将其确认为:

git config --global diff.tool p4merge
git config --global difftool.p4merge.cmd 'p4merge.exe $LOCAL $REMOTE'
git config --global difftool.prompt false

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge.exe $BASE $LOCAL $REMOTE $MERGED'
git config --global mergetool.prompt false
git config --global mergetool.trustExitCode false
git config --global mergetool.keepBackup false

I recommend you stick to "git config" instead of editing the conf manually because hyphens etc sometimes are different between these methods.

我建议您坚持使用“git config”而不是手动编辑 conf,因为这些方法之间的连字符等有时是不同的。

回答by james.garriss

Dan Limerick has an excellent short tutorial on setting up Perforce's p4merge on Windows:

Dan Limerick 有一个关于在 Windows 上设置 Perforce 的 p4merge 的优秀简短教程:

http://danlimerick.wordpress.com/2011/06/19/git-for-window-tip-use-p4merge-as-mergetool/

http://danlimerick.wordpress.com/2011/06/19/git-for-window-tip-use-p4merge-as-mergetool/

Tested with Git 1.9.4 and p4merge 2014.1 on Windows 7.

在 Windows 7 上使用 Git 1.9.4 和 p4merge 2014.1 进行测试。

回答by wengeezhang

  1. git config --global diff.tool p4merge
  2. git config --global difftool.p4merge.cmd "p4merge \"$LOCAL\" \"$REMOTE\""
  3. add p4merge.exe path("C:\Program Files\Perforce\p4merge.exe") to environment path
  1. git config --global diff.tool p4merge
  2. git config --global difftool.p4merge.cmd "p4merge \"$LOCAL\" \"$REMOTE\""
  3. 将 p4merge.exe 路径(“C:\Program Files\Perforce\p4merge.exe”)添加到环境路径

ps:

附:

  • in step 2,you've to add \"$LOCAL\" \"$REMOTE\"(local diff remote),or p4merge won't know what to diff
  • 在第 2 步中,您必须添加\"$LOCAL\" \"$REMOTE\"(本地差异远程),否则 p4merge 将不知道要差异什么

回答by Sunil Shahu

I personally like meld better from other open source tools for just diff,

我个人更喜欢从其他开源工具中融合更好的差异,

Expanding @wengeezhang answer, here is the step to configure Meld as difftool.

扩展@wengeezhang 答案,这里是将 Meld 配置为 difftool 的步骤。

  1. Add Meld.exe path("C:\Program Files (x86)\Meld") to environment path
  2. Fire up windows command prompt
  3. git config --global diff.tool meld
  4. git config --global difftool.meld.cmd "meld \"$LOCAL\" \"$REMOTE\""
  1. 将 Meld.exe path("C:\Program Files (x86)\Meld") 添加到环境路径
  2. 启动 Windows 命令提示符
  3. git config --global diff.tool meld
  4. git config --global difftool.meld.cmd "meld \"$LOCAL\" \"$REMOTE\""

Bonus tip: Go to Git repo's root directory and fire up following command to see all diff side by side

额外提示:转到 Git 存储库的根目录并启动以下命令以并排查看所有差异

git difftool --dir-diff ./