Windows 上的 Git:如何设置合并工具?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/426026/
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
Git on Windows: How do you set up a mergetool?
提问by Jake
I've tried msysGit and Git on Cygwin. Both work just fine in and of themselves and both run gitk and git-gui perfectly.
我在 Cygwin 上尝试过 msysGit 和 Git。两者都可以正常工作,并且都可以完美地运行 gitk 和 git-gui。
Now how the heck do I configure a mergetool? (Vimdiff works on Cygwin, but preferably I would like something a little more user-friendly for some of our Windows-loving coworkers.)
现在我该如何配置合并工具?(Vimdiff 在 Cygwin 上工作,但最好我想要一些对我们一些喜欢 Windows 的同事更用户友好的东西。)
采纳答案by Milan Gardian
To follow-up on Charles Bailey's answer, here's my git setup that's using p4merge(free cross-platform 3way merge tool); tested on msys Git (Windows) install:
为了跟进 Charles Bailey 的回答,这是我使用p4merge(免费跨平台 3way 合并工具)的 git 设置;在 msys Git (Windows) 安装上测试:
git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'
or, from a windows cmd.exe shell, the second line becomes :
或者,从 windows cmd.exe shell 中,第二行变为:
git config --global mergetool.p4merge.cmd "p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""
The changes (relative to Charles Bailey):
变化(相对于 Charles Bailey):
- added to global git config, i.e. valid for all git projects not just the current one
- the custom tool config value resides in "mergetool.[tool].cmd", not "merge.[tool].cmd" (silly me, spent an hour troubleshooting why git kept complaining about non-existing tool)
- added double quotes for all file names so that files with spaces can still be found by the merge tool (I tested this in msys Git from Powershell)
- note that by default Perforce will add its installation dir to PATH, thus no need to specify full path to p4merge in the command
- 添加到全局 git 配置中,即对所有 git 项目有效,而不仅仅是当前项目
- 自定义工具配置值驻留在“mergetool.[tool].cmd”中,而不是“merge.[tool].cmd”(愚蠢的我,花了一个小时来解决为什么 git 一直抱怨不存在的工具)
- 为所有文件名添加了双引号,以便合并工具仍然可以找到带有空格的文件(我在 Powershell 的 msys Git 中对此进行了测试)
- 请注意,默认情况下,Perforce 会将其安装目录添加到 PATH,因此无需在命令中指定 p4merge 的完整路径
Download: http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools
下载:http: //www.perforce.com/product/components/perforce-visual-merge-and-diff-tools
EDIT(Feb 2014)
编辑(2014 年 2 月)
As pointed out by @Gregory Pakosz, latest msys gitnow "natively" supports p4merge(tested on 1.8.5.2.msysgit.0).
正如@Gregory Pakosz所指出的,最新的msys git现在“本地”支持p4merge(在1.8.5.2.msysgit.0上测试)。
You can display list of supported tools by running:
您可以通过运行来显示支持的工具列表:
git mergetool --tool-help
You should see p4mergein either availableor validlist. If not, please update your git.
您应该在可用或有效列表中看到p4merge。如果没有,请更新您的 git。
If p4mergewas listed as available, it is in your PATHand you only have to set merge.tool:
如果p4merge被列为可用,则它在您的PATH 中,您只需设置merge.tool:
git config --global merge.tool p4merge
If it was listed as valid, you have to define mergetool.p4merge.pathin addition to merge.tool:
如果它被列为有效,你必须定义mergetool.p4merge.path除了merge.tool:
git config --global mergetool.p4merge.path c:/Users/my-login/AppData/Local/Perforce/p4merge.exe
- The above is an example path when p4merge was installed for the current user, not system-wide (does not need admin rights or UAC elevation)
- Although
~
should expand to current user's home directory (so in theory the path should be~/AppData/Local/Perforce/p4merge.exe
), this did not work for me - Even better would have been to take advantage of an environment variable (e.g.
$LOCALAPPDATA/Perforce/p4merge.exe
), git does not seem to be expanding environment variables for paths (if you know how to get this working, please let me know or update this answer)
- 以上是为当前用户安装 p4merge 时的示例路径,不是系统范围的(不需要管理员权限或 UAC 提升)
- 虽然
~
应该扩展到当前用户的主目录(所以理论上路径应该是~/AppData/Local/Perforce/p4merge.exe
),但这对我不起作用 - 更好的是利用环境变量(例如
$LOCALAPPDATA/Perforce/p4merge.exe
),git 似乎没有为路径扩展环境变量(如果您知道如何使其工作,请告诉我或更新此答案)
回答by daizuozhuo
setting mergetool.p4merge.cmd will not work anymore since Git has started trying to support p4merge, see libexec/git-core/git-mergetool--lib.so we just need to specify the mergetool path for git,for example the p4merge:
由于 Git 已经开始尝试支持 p4merge,因此设置 mergetool.p4merge.cmd 将不再起作用,请参阅 libexec/git-core/git-mergetool--lib。所以我们只需要指定 git 的合并工具路径,例如 p4merge:
git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'
git config --global merge.tool p4merge
Then it will work.
然后它会起作用。
回答by Dɑv?d
I'm using Portable Git on WinXP (works a treat!), and needed to resolve a conflict that came up in branching. Of all the gui's I checked, KDiff3proved to be the most transparent to use.
我在 WinXP 上使用便携式 Git(很好用!),并且需要解决分支中出现的冲突。在我检查过的所有 gui 中,KDiff3被证明是最透明的。
But I found the instructions I needed to get it working in Windows in this blog post, instructions which differ slightly from the other approaches listed here. It basically amounted to adding these lines to my .gitconfig
file:
但是我在这篇博文中找到了让它在 Windows 中运行所需的说明,这些说明与此处列出的其他方法略有不同。它基本上相当于将这些行添加到我的.gitconfig
文件中:
[merge]
tool = kdiff3
[mergetool "kdiff3"]
path = C:/YourPathToBinaryHere/KDiff3/kdiff3.exe
keepBackup = false
trustExitCode = false
Working nicely now!
现在工作得很好!
回答by splicer
Under Cygwin, the onlything that worked for me is the following:
在 Cygwin 下,唯一对我有用的是以下内容:
git config --global merge.tool myp4merge
git config --global mergetool.myp4merge.cmd 'p4merge.exe "$(cygpath -wla $BASE)" "$(cygpath -wla $LOCAL)" "$(cygpath -wla $REMOTE)" "$(cygpath -wla $MERGED)"'
git config --global diff.tool myp4diff
git config --global difftool.myp4diff.cmd 'p4merge.exe "$(cygpath -wla $LOCAL)" "$(cygpath -wla $REMOTE)"'
Also, I like to turn off the prompt message for difftool:
另外,我喜欢关闭difftool的提示信息:
git config --global difftool.prompt false
回答by CB Bailey
git mergetool
is fully configurable so you can pretty much chose your favourite tool.
git mergetool
是完全可配置的,因此您几乎可以选择自己喜欢的工具。
The full documentation is here: http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html
完整文档在这里:http: //www.kernel.org/pub/software/scm/git/docs/git-mergetool.html
In brief, you can set a default mergetool by setting the user config variable merge.tool
.
简而言之,您可以通过设置用户配置变量来设置默认合并工具merge.tool
。
If the merge tool is one of the ones supported natively by it you just have to set mergetool.<tool>.path
to the full path to the tool (replace <tool>
by what you have configured merge.tool
to be.
如果合并工具是它本机支持的工具之一,您只需设置mergetool.<tool>.path
该工具的完整路径(替换<tool>
为您配置的内容merge.tool
。
Otherwise, you can set mergetool.<tool>.cmd
to a bit of shell to be eval'ed at runtime with the shell variables $BASE, $LOCAL, $REMOTE, $MERGED
set to the appropriate files. You have to be a bit careful with the escaping whether you directly edit a config file or set the variable with the git config
command.
否则,您可以设置mergetool.<tool>.cmd
一些 shell,以便在运行时将 shell 变量$BASE, $LOCAL, $REMOTE, $MERGED
设置为适当的文件进行评估。无论是直接编辑配置文件还是使用git config
命令设置变量,都必须小心转义。
Something like this should give the flavour of what you can do ('mymerge' is a fictional tool).
像这样的东西应该让你知道你可以做什么('mymerge' 是一个虚构的工具)。
git config merge.tool mymerge
git config merge.mymerge.cmd 'mymerge.exe --base "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"'
Once you've setup your favourite merge tool, it's simply a matter of running git mergetool
whenever you have conflicts to resolve.
一旦您设置了您最喜欢的合并工具,git mergetool
只要您有冲突需要解决,就只需运行即可。
The p4merge tool from Perforce is a pretty good standalone merge tool.
Perforce 的 p4merge 工具是一个非常好的独立合并工具。
回答by developer747
For beyond compare on Windows 7
无法在 Windows 7 上进行比较
git config --global merge.tool bc3
git config --global mergetool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\BCompare.exe"
回答by friederbluemle
It seems that newer git versions support p4merge directly, so
似乎较新的 git 版本直接支持 p4merge,所以
git config --global merge.tool p4merge
should be all you need, if p4merge.exe is on your path. No need to set up cmd or path.
如果 p4merge.exe 在您的路径上,应该就是您所需要的。无需设置 cmd 或路径。
回答by hlovdal
回答by Mike Glenn
I had to drop the extra quoting using msysGit on windows 7, not sure why.
我不得不在 Windows 7 上使用 msysGit 删除额外的引用,不知道为什么。
git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge $BASE $LOCAL $REMOTE $MERGED'
回答by idbrii
If you're doing this through cygwin, you may need to use cygpath:
如果您通过 cygwin 执行此操作,则可能需要使用 cygpath:
git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd 'p4merge `cygpath -w $BASE` `cygpath -w $LOCAL` `cygpath -w $REMOTE` `cygpath -w $MERGED`'