git 如何使用 msysgit/gitk 设置 DiffMerge?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/780425/
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
How do I setup DiffMerge with msysgit / gitk?
提问by average_geek
I've just started using Git and it's possible I've missed something obvious, but here goes:
我刚刚开始使用 Git,可能我错过了一些明显的东西,但这里有:
- I'm using msysgit 1.6.2.2 on Windows XP
- While installing, I picked option 1 to "Use Git Bash only"
- 我在 Windows XP 上使用 msysgit 1.6.2.2
- 安装时,我选择了“仅使用 Git Bash”的选项 1
I'm trying to put together a wrapper script that I can use to replace the built in git diff with DiffMerge. Based on this threadon SO, I created the following batch file:
我正在尝试将一个包装脚本放在一起,我可以用它来用 DiffMerge 替换内置的 git diff。基于SO上的这个线程,我创建了以下批处理文件:
@echo off
REM ---- Switch forward slashes to back slashes ----
set oldW=%2
set oldW=%oldW:/=\%
set newW=%5
set newW=%newW:/=\%
REM ---- Launch DiffMerge ----
"C:/Programs/SourceGear/DiffMerge/DiffMerge.exe" /title1="Old Version" %oldW% /title2="New Version" %newW%
I placed the bat file under %GIT_INSTALL%/cmd and edited my .gitconfig file as follows:
我将 bat 文件放在 %GIT_INSTALL%/cmd 下并按如下方式编辑我的 .gitconfig 文件:
[diff]
external = C:/Programs/git/cmd/git-diff-wrapper.bat
If i launch Git Bash and execute git diff HEAD HEAD~ -- myfile
如果我启动 Git Bash 并执行 git diff HEAD HEAD~ -- myfile
I get a message File (\dev\null) not found
- which given I'm on Windows is not surprising.
我收到一条消息File (\dev\null) not found
- 鉴于我在 Windows 上,这并不奇怪。
Pressing on, I launched gitk and under Edit>Preferences, I chose the same wrapper script. Trying the "external diff" option for a particular file gives the cryptic error message Unknown Option "
按下后,我启动了 gitk,在 Edit>Preferences 下,我选择了相同的包装脚本。为特定文件尝试“外部差异”选项会给出神秘的错误消息Unknown Option "
Clearly, I have no idea what I'm doing anymore so any help would be much appreciated.
显然,我不知道我在做什么了,所以任何帮助将不胜感激。
采纳答案by VonC
I just experienced a somewhat similar experience with setting Notepad++ as my external editor with msysgit1.6.2.2.
我刚刚在使用 msysgit1.6.2.2 将 Notepad++ 设置为我的外部编辑器时遇到了类似的经历。
The key was to realize the wrapper was not a DOS script, but a /bin/sh script.
关键是要意识到包装器不是 DOS 脚本,而是 /bin/sh 脚本。
So try to put in your ".bat" (even though it is not exactly a bat script, the extension is not important here):
所以试着输入你的“.bat”(即使它不完全是一个 bat 脚本,扩展名在这里并不重要):
#!/bin/sh
# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode
"C:/Programs/SourceGear/DiffMerge/DiffMerge.exe" /title1="Old Version" "" /title2="New Version" "" | cat
Do not worry about making all the '\
' go '/
': it is done by the Git scripts calling the external diff tool.
不要担心让所有 ' \
' go ' ' /
':它是由调用外部差异工具的 Git 脚本完成的。
I did not test it with DiffMerge, but with WinMerge, it works just fine, both from a DOS session or a Git Shell.
我没有使用 DiffMerge 测试它,但是使用 WinMerge,它在 DOS 会话或 Git Shell 中都可以正常工作。
#!/bin/sh
"C:/Program Files/WinMerge/WinMergeU.exe" -e -ub "" "" | cat
(with the '-e
' option, I have just ot type on 'ESC
' to close and quit the diff tool: that works great!)
(使用“ -e
”选项,我只需输入“ ESC
”即可关闭并退出差异工具:效果很好!)
average_geekadds in the comments:
average_geek在评论中补充道:
added the '
/bin/sh
' header and tried running git diff again.
This time the error is:Unexpected parameter 'C:/Docume~/avggeek/LOCALS~1/Temp/.diff_b08444
Is there a way to see what are the parameters getting passed when I callgit diff
?
添加了 '
/bin/sh
' 标头并再次尝试运行 git diff。
这次的错误是:Unexpected parameter 'C:/Docume~/avggeek/LOCALS~1/Temp/.diff_b08444
有没有办法查看调用时传递的参数是什么git diff
?
1/ There actually is a way to see what are the parameters getting passed!
Add the following line in the C:\Program Files\Git\libexec\git-core\git-sh-setup
file:
1/ 实际上有一种方法可以查看传递的参数是什么!
在C:\Program Files\Git\libexec\git-core\git-sh-setup
文件中添加以下行:
git_editor() {
: "${GIT_EDITOR:=$(git config core.editor)}"
: "${GIT_EDITOR:=${VISUAL:-${EDITOR}}}"
case "$GIT_EDITOR,$TERM" in
,dumb)
echo >&2 "No editor specified in GIT_EDITOR, core.editor, VISUAL,"
echo >&2 "or EDITOR. Tried to fall back to vi but terminal is dumb."
echo >&2 "Please set one of these variables to an appropriate"
echo >&2 "editor or run "/title1="Old Version"" "" "/title2="New Version"" ""
or
"/title1=\"Old Version\"" "" "/title2=\"New Version\"" ""
with options that will not cause an"
echo >&2 "editor to be invoked (e.g., -m or -F for git-commit)."
exit 1
;;
esac
#### ADD THIS LINE BELOW
echo >&2 "editor is ${GIT_EDITOR:=vi} $@."
#### END ADDITION ABOVE
eval "${GIT_EDITOR:=vi}" '"$@"'
}
You will see what editor is being called, with what parameter.
您将看到正在使用什么参数调用什么编辑器。
Now, regarding the "Unexpected parameter" part:
I did have the same kind of error when I called WinMergeU.exe with "/e /ub
" instead of "-e -ub
", so first question is:
Are you sure that the "/title1
" bit could not be used as "-title1
" or "-t1
" or "--title1
" or "--t1
" ? That is what Is can see from the chapter 9 "Command Lines Arguments" of the pdf documentation of DiffMerge.
If not, I suspect some double quotes are in order for delimiting properly the different parameters. Something like:
现在,关于“意外参数”部分:
当我用“ /e /ub
”而不是“ -e -ub
”调用 WinMergeU.exe 时,我确实遇到了同样的错误,所以第一个问题是:
你确定“ /title1
”位不能用作“ -title1
”还是“ -t1
”还是“ --title1
”还是“ --t1
”?这就是 Is 可以从DiffMerge 的 pdf 文档的第 9 章“命令行参数”中看到的内容。
如果没有,我怀疑一些双引号是为了正确分隔不同的参数。就像是:
-t1="Old Version" "" -t2="New Version" ""
But my money would rather be on the "-title1
" or "-t1
" form:
但我的钱更愿意用“ -title1
”或“ -t1
”的形式:
[diff]
tool = diffm
[difftool "diffm"]
cmd = "H:/PROGRA~1/SourceGear/DiffMerge/DiffMerge.exe $LOCAL $REMOTE"
prompt = false
[merge]
tool = diffmerge
[mergetool "diffmerge"]
cmd = "H:/PROGRA~1/SourceGear/DiffMerge/DiffMerge.exe --merge --result=$MERGED $LOCAL $BASE $REMOTE"
trustExitCode = false
keepBackup = false
should work just fine.
应该工作得很好。
回答by Wavesailor
I looked all over the internet for the answer to this. I tried all of the solutions above and elsewhere. I could get the merge part to work and not the diff part or vice versa. So what I did eventually was to hack together my own simple solutionfrom all the info I got on the internet which works for me.
It does not require any scripts only that you edit your .gitconfig
which normally resides in the following directory C:\Documents and Settings\[username]
You'll need to have DiffMerge program installed already.
我在互联网上四处寻找答案。我尝试了上面和其他地方的所有解决方案。我可以让合并部分工作,而不是差异部分,反之亦然。所以我最终做的是从我在互联网上获得的所有信息中找出我自己的简单解决方案,这些信息对我有用。它不需要任何脚本,只需要您编辑.gitconfig
通常位于以下目录中的脚本C:\Documents and Settings\[username]
您需要已经安装了 DiffMerge 程序。
Here is relevant extract of my .gitconfig
file. You'll just need to edit the path to where your version of of DiffMerge
is. Notethat I used the old DOS 8.3 format in the path
这是我的.gitconfig
文件的相关摘录。您只需要编辑您的版本所在的路径DiffMerge
。请注意,我在路径中使用了旧的 DOS 8.3 格式
cmd = "C:/PROGRA~1/SourceGear/Common/DiffMerge/sgdm.exe ...etc..."
You can also set it up using the commands git config --replace --global [options]
if you so wish.
git config --replace --global [options]
如果您愿意,也可以使用命令进行设置。
This simple solution works perfectly for me, too. For more recent versions of DiffMerge (3.3.1), the command path needs to be changed:
这个简单的解决方案也非常适合我。对于较新版本的 DiffMerge (3.3.1),需要更改命令路径:
[merge]
tool = diffmerge
[mergetool "diffmerge"]
cmd = \"C:/Program Files/git/cmd/git-diffmerge-merge.sh\" \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
trustExitCode = false
回答by Max
This works for me with as follows:
这对我有用,如下所示:
In ~/.gitconfig
:
在~/.gitconfig
:
#!/bin/sh
localPath=""
basePath=""
remotePath=""
resultPath=""
if [ ! -f $basePath ]
then
basePath="~/diffmerge-empty"
fi
"C:/Program Files/SourceGear/DiffMerge/DiffMerge.exe" --merge --result="$resultPath" "$localPath" "$basePath" "$remotePath" --title1="Mine" --title2="Merged: " --title3="Theirs"
In C:\Program Files\Git\cmd\git-diffmerge-merge.sh
:
在C:\Program Files\Git\cmd\git-diffmerge-merge.sh
:
"C:/Programs/SourceGear/DiffMerge/DiffMerge.exe" -t1="Old Version" -t2="New Version"
Part of the credit goes to http://therightstuff.de/2009/01/28/Setting-Up-SourceGear-DiffMerge-With-Git.aspx;)
部分功劳归于http://therightstuff.de/2009/01/28/Setting-Up-SourceGear-DiffMerge-With-Git.aspx;)
回答by average_geek
VonC - switching to -t1 and -t2 fixed the errors. Diffmerge now works for git bash :)
VonC - 切换到 -t1 和 -t2 修复了错误。Diffmerge 现在适用于 git bash :)
After a little bit of poking at the gitk patchthat added External Diff support, I realized that it's calling an External Diff program directly with the two files as arguments. So I modified gitk>Edit>Preferences and put the following command directly into the External Diff Tool option:
在对添加了 External Diff 支持的 gitk 补丁进行了一些研究之后,我意识到它直接使用两个文件作为参数调用了一个 External Diff 程序。所以我修改了 gitk>Edit>Preferences 并将以下命令直接放入 External Diff Tool 选项中:
##代码##Now I have DiffMerge working for gitk too :-)
现在我也有 DiffMerge 为 gitk 工作 :-)