在 Git 中使用 Winmerge 来归档差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881594/
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
use Winmerge inside of Git to file diff
提问by eiu165
Is there a way to use Winmerge inside of git to do Diffs?
有没有办法在 git 中使用 Winmerge 来做 Diffs?
回答by VonC
Update June 2015, 6 years later:
6 年后,2015 年 6 月更新:
As detailed in "git mergetool winmerge", a simple git config diff.tool winmerge
will be enough.
如“ git mergetool winmerge”中所述,简单git config diff.tool winmerge
就足够了。
Git 2.5+ (Q2, 2015) is now aware of Winmerge as a diff or merge tool!
Git 2.5+(2015 年第二季度)现在知道 Winmerge 作为差异或合并工具!
Original answer (2009-2012)
原始答案(2009-2012)
(msysgit, 1.6.5, DOS session)
(msysgit, 1.6.5, DOS 会话)
The first part (using winmerge) is described in "How do I view ‘git diff' output with visual diff program?"
第一部分(使用 winmerge)在“如何使用可视化差异程序查看 'git diff' 输出?”中进行了描述。
C:\myGitRepo>git config --replace --global diff.tool winmerge
C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
C:\myGitRepo>git config --replace --global difftool.prompt false
With winmerge.sh
stored in a directory part of your PATH
:
随着winmerge.sh
存储在您的目录部分PATH
:
#!/bin/sh
echo Launching WinMergeU.exe:
"$PROGRAMFILES/WinMerge/WinMergeU.exe" -e -u -dl "Local" -dr "Remote" "" ""
(see WinMerge Command-line options)
(请参阅WinMerge 命令行选项)
git difftool
will now launch WinMerge.
If you want git diff
to launch WinMerge, just set:
现在将启动 WinMerge。
如果要git diff
启动 WinMerge,只需设置:
set GIT_EXTERNAL_DIFF=winmerge.sh
But the real added value comes from the ability to use that same diff tool to present all differences in one batchinstead of presenting them sequentially, forcing you to close the diff tool windows one file at a time.
但真正的附加价值来自于使用相同的差异工具在一批中呈现所有差异的能力,而不是按顺序呈现它们,迫使您一次关闭一个文件的差异工具窗口。
Update June 2012(2-and-a-half years later):
2012 年 6 月更新(两年半后):
Comparing directories instead of file-by-file will be available soon:
See [ANNOUNCE] Git 1.7.11.rc1:
比较目录而不是逐个文件将很快可用:
请参阅[ANNOUNCE] Git 1.7.11.rc1:
"
git difftool
" learned the "--dir-diff
" option to spawn external diff tools that can compare two directory hierarchiesat a time after populating two temporary directories, instead of running an instance of the external tool once per a file pair.
"
git difftool
"学习了"--dir-diff
" 选项来生成外部差异工具,可以在填充两个临时目录后一次比较两个目录层次结构,而不是每个文件对运行一次外部工具实例。
See "Patch difftool
: teach difftool
to handle directory diffs", and the answer "Directory comparison of Git branches" for more details.
有关更多详细信息,请参阅“补丁difftool
:教difftool
处理目录差异”和答案“ Git 分支的目录比较”。
Original difftool by directories script (December 2009)
目录脚本的原始 difftool(2009 年 12 月)
As Seba Illingworthmentions in his answer, a script git-diffall.sh (also put in the path) can do just that:
正如Seba Illingworth在他的回答中提到的,脚本 git-diffall.sh (也放在路径中)可以做到这一点:
#!/bin/sh
git diff --name-only "$@" | while read filename; do
git difftool "$@" --no-prompt "$filename" &
done
But that only works by opening nwindows for nfiles (if you try to use the -s
option of WinMerge, it will not work because of the temp files being deleted by the difftool too early)
但这只能通过为n 个文件打开n 个窗口来工作(如果您尝试使用WinMerge 选项,它将无法工作,因为 difftool 过早删除了临时文件)-s
That is why I like the approach of GitDiff.bat - power-diffing with GI, which allows you to review the list of files with a difference, before selecting one to examine its internal differences.
I have tweaked it to use only DOS commands
这就是为什么我喜欢GitDiff.bat的方法- 使用 GI 进行功率差异,它允许您在选择一个文件以检查其内部差异之前查看具有差异的文件列表。
我已将其调整为仅使用 DOS 命令
@echo off
setlocal
if "%1" == "-?" (
echo GitDiff - enables diffing of file lists, instead of having to serially
echo diff files without being able to go back to a previous file.
echo Command-line options are passed through to git diff.
echo If GIT_FOLDER_DIFF is set, it is used to diff the file lists. Default is windff.
goto END
)
if "%GIT_DIFF_COPY_FILES%"=="" (
rd /s /q %TEMP%\GitDiff
mkdir %TEMP%\GitDiff
mkdir %TEMP%\GitDiff\old
mkdir %TEMP%\GitDiff\new
REM This batch file will be called by git diff. This env var indicates whether it is
REM being called directly, or inside git diff
set GIT_DIFF_COPY_FILES=1
set GIT_DIFF_OLD_FILES=%TEMP%\GitDiff\old
set GIT_DIFF_NEW_FILES=%TEMP%\GitDiff\new
set GIT_EXTERNAL_DIFF=%~dp0\GitDiff.bat
echo Please wait and press q when you see "(END)" printed in reverse color...
call git diff %*
if defined GIT_FOLDER_DIFF (
REM This command using GIT_FOLDER_DIFF just does not work for some reason.
%GIT_FOLDER_DIFF% %TEMP%\GitDiff\old %TEMP%\GitDiff\new
goto END
)
if exist "%ProgramFiles%\Beyond Compare 2\BC2.exe" (
set GIT_FOLDER_DIFF="%ProgramFiles%\Beyond Compare 2\BC2.exe"
"%ProgramFiles%\Beyond Compare 2\BC2.exe" %TEMP%\GitDiff\old %TEMP%\GitDiff\new
goto END
)
"%ProgramFiles(x86)%\WinMerge\WinMergeU.exe" -r -e -dl "Local" -dr "Remote" %TEMP%\GitDiff\old %TEMP%\GitDiff\new
goto END
)
REM diff is called by git with 7 parameters:
REM path old-file old-hex old-mode new-file new-hex new-mode
copy %TEMP%\%~nx2 %GIT_DIFF_OLD_FILES%\%1
copy %5 %GIT_DIFF_NEW_FILES%
:END
It is not robust enough to handle files with same names in different directories, but it gives you a general idea of what is possible:
Here only one WinMerge will open up, with the list of files having internal differences. You can click on the ones you want to examines, then a simple ESCwill close the all WinMerge-diff
session.
它不足以处理不同目录中具有相同名称的文件,但它可以让您大致了解可能的情况:
这里只会打开一个 WinMerge,文件列表具有内部差异。您可以单击要检查的那些,然后一个简单的ESC将关闭所有WinMerge-diff
会话。
回答by Gopi
I faced trouble using the first part in 2 places and fixed it as follows
我在 2 个地方使用第一部分时遇到了问题,并按如下方式修复了它
The second command for setting up winmerge.cmd required an extra slash on cmdline (before $LOCAL and $REMOTE), else cygwin was substituting the variable in cmdline
C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
changed the winmerge.sh file to (without this, was getting right-path-invalid error)
#!/bin/sh echo Launching WinMergeU.exe: "$(cygpath -aw "")" "$(cygpath -aw "")" "$PROGRAMFILES/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$(cygpath -aw "")" "$(cygpath -aw "")"
用于设置 winmerge.cmd 的第二个命令需要在 cmdline 上添加一个额外的斜线(在 $LOCAL 和 $REMOTE 之前),否则 cygwin 将替换 cmdline 中的变量
C:\myGitRepo>git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
将 winmerge.sh 文件更改为(没有这个,就会出现正确路径无效错误)
#!/bin/sh echo Launching WinMergeU.exe: "$(cygpath -aw "")" "$(cygpath -aw "")" "$PROGRAMFILES/WinMerge/WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$(cygpath -aw "")" "$(cygpath -aw "")"
回答by robertcollier4
Since the thread is getting confusing and bifurcated, here are consolidated instructions for the Directory Listing "--dir-diff" WinMerge method for msysgit Git Windows.
由于线程变得混乱和分叉,这里是 msysgit Git Windows 的目录列表“--dir-diff”WinMerge 方法的合并说明。
Step 1- Create a file named winmerge.sh at a location accessible to your path (such as /home/bin/winmerge.sh) with following contents.
步骤 1- 在您的路径可访问的位置(例如 /home/bin/winmerge.sh)创建一个名为 winmerge.sh 的文件,其中包含以下内容。
#!/bin/sh
echo Launching WinMergeU.exe:
"$PROGRAMFILES/WinMerge/WinMergeU.exe" -r -ub -dl "Remote" -dr "Local" "" ""
Step 2- Type the following commands in Git Bash to instruct git to use winmerge.sh as difftool (these options get stored in /home/.gitconfig):
第 2 步- 在 Git Bash 中键入以下命令以指示 git 使用 winmerge.sh 作为 difftool(这些选项存储在 /home/.gitconfig 中):
git config --replace --global diff.tool winmerge
git config --replace --global difftool.winmerge.cmd "winmerge.sh \"$LOCAL\" \"$REMOTE\""
git config --replace --global difftool.prompt false
Step 3- Now you can test by typing the following command in Git Bash to start your WinMerge diff:
第 3 步- 现在您可以通过在 Git Bash 中键入以下命令来启动您的 WinMerge diff 进行测试:
git difftool --dir-diff
Step 4- For quicker access, create an alias for this command by adding this line to .bashrc in your home folder (or create .bashrc file with this line if file does not already exist):
第 4 步- 为了更快地访问,通过将此行添加到您的主文件夹中的 .bashrc 来为此命令创建别名(如果文件尚不存在,则使用此行创建 .bashrc 文件):
alias diffdir='git difftool --dir-diff'
Step 5- Now you can quickly see a diff in WinMerge just by typing the following command into Git Bash
第 5 步- 现在您只需在 Git Bash 中输入以下命令即可快速查看 WinMerge 中的差异
diffdir
回答by Alf47
I have a script that will set the Diff and Merge tools in the Git config with the proper parameters that doesn't require a separate .sh file to exist. It seems to be working fine for me.
我有一个脚本,它将使用不需要单独的 .sh 文件的适当参数在 Git 配置中设置 Diff 和 Merge 工具。对我来说似乎工作正常。
git config --global diff.tool winmerge
git config --global difftool.prompt false
git config --global difftool.winmerge.cmd "\"$PROGRAMFILES\"/WinMerge/WinMergeU.exe -r -u -e -dl \"Local\" -dr \"Remote\" \"$LOCAL\" \"$REMOTE\""
git config --global merge.tool winmerge
git config --global mergetool.prompt false
git config --global mergetool.winmerge.trustExitCode true
git config --global mergetool.winmerge.cmd "\"$PROGRAMFILES\"/WinMerge/WinMergeU.exe -r -u -e -dl \"Local\" -dr \"Remote\" \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\""
Note- the entire .cmd portion is quoted so that the parameters will be listed in the .gitconfig properly
注意- 整个 .cmd 部分被引用,以便参数将正确列在 .gitconfig 中
回答by John Bentley
Without configuration:
无配置:
git difftool --tool winmerge
Assuming:
假设:
- Winmerge is installed
- Git for windows is installed, from "git version 2.12.0.windows1" or above (although earlier versions of git may have introduced the command).
- Winmerge 已安装
- 安装了适用于 windows 的 Git,从“git version 2.12.0.windows1”或更高版本开始(尽管早期版本的 git 可能已经引入了该命令)。
回答by Caner
On windows you can do it this way:
在 Windows 上,您可以这样做:
1) Open .gitconfig file. It's located at your home directory: c:\users\username.gitconfig
1) 打开 .gitconfig 文件。它位于您的主目录:c:\users\username.gitconfig
2) Add the lines below. Pay attention to the single quotes wrapping the path to winmerge:
2)添加下面的行。注意包裹winmerge路径的单引号:
[diff]
tool = winmerge
[difftool "winmerge"]
cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e "$LOCAL" "$REMOTE"
[difftool]
prompt = false
[merge]
tool = winmerge
[mergetool "winmerge"]
cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" \"$MERGED\" \"$REMOTE\"
[mergetool]
keepBackup = false
trustExitCode = false
回答by Shawn South
I was confused about why the solution was presented as a DOS batch file, as my Git installation came with a bash shell. I was also unable to get a DOS context running from bash, so I've attempted to adapt what was previously shared in a bash context.
我对为什么将解决方案显示为 DOS 批处理文件感到困惑,因为我的 Git 安装附带了一个 bash shell。我也无法从 bash 运行 DOS 上下文,因此我尝试调整以前在 bash 上下文中共享的内容。
Since git diff
appears to run the specified command once for each file, I split my solution into two bash scripts:
由于git diff
似乎为每个文件运行一次指定的命令,我将我的解决方案拆分为两个 bash 脚本:
First, configure gitprepdiff.sh
to be the difftool as previously mentioned
首先,配置gitprepdiff.sh
为前面提到的difftool
#!/bin/sh
#echo ...gitprepdiff.sh
cp -v "$TMP/GitDiff/old/"
cp -v "$TMP/GitDiff/new"
I also noted that the results of the git configure
commands can be found and edited directly in C:\Users\<username>\.gitconfigure
我还注意到git configure
命令的结果可以直接在C:\Users\<username>\.gitconfigure
gitdiff.sh
is then run at the command-line where you would normally call git diff
gitdiff.sh
然后在您通常调用的命令行中运行 git diff
#!/bin/sh
#echo Running gitdiff.sh...
DIFFTEMP=$TMP/GitDiff
echo Deleting and re-creating $DIFFTEMP...
rm -rf $DIFFTEMP;
mkdir $DIFFTEMP;
echo Creating $DIFFTEMP/old...
mkdir $DIFFTEMP/old;
echo Creating $DIFFTEMP/new...
mkdir $DIFFTEMP/new;
git diff --name-only "$@" | while read filename; do
git difftool "$@" --no-prompt "$filename";
done
"$PROGRAMFILES\WinMerge\WinMergeU.exe" -r -e -dl "Repository" -dr "Working" $LOCALAPPDATA\Temp\1\GitDiff\old $LOCALAPPDATA\Temp\1\GitDiff\new
Also worth noting is that, on my installation, /tmp
(in bash) mapped to %LOCALAPPDATA%\Temp\1\
(in Windows), so that's why I'm using the latter in my call to WinMerge.
另外值得注意的是,在我的安装中,/tmp
(在 bash 中)映射到%LOCALAPPDATA%\Temp\1\
(在 Windows 中),所以这就是我在调用 WinMerge 时使用后者的原因。
回答by Steve Lang
git config --global diff.tool winmerge
git config --global difftool.winmerge.cmd "\"$PROGRAMFILES\WinMerge\WinMergeU.exe\" -u -dl \"Local\" -dr \"Remote\" \"$LOCAL\" \"$REMOTE\""
git config --global difftool.prompt false
As per the WinMerge command line manual: "Parameters are prefixed with either a forward slash ( / ) or dash ( - ) character"
根据WinMerge 命令行手册:“参数以正斜杠 ( / ) 或破折号 ( - ) 字符为前缀”