Windows 上的 Git:如何设置合并工具? [英] Git on Windows: How do you set up a mergetool?

查看:45
本文介绍了Windows 上的 Git:如何设置合并工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Cygwin 上尝试过 msysGit 和 Git.两者都可以正常工作,并且都可以完美地运行 gitk 和 git-gui.

现在我该如何配置合并工具?(Vimdiff 可在 Cygwin 上运行,但最好我希望对我们一些喜欢 Windows 的同事更友好一些.)

解决方案

为了跟进 Charles Bailey 的回答,这是我使用 p4merge(免费的跨平台 3way 合并工具);在 msys Git (Windows) 安装上测试:

git config --global merge.tool p4mergegit config --global mergetool.p4merge.cmd 'p4merge.exe "$BASE" "$LOCAL" "$REMOTE" "$MERGED"'

或者,在 windows cmd.exe shell 中,第二行变成:

git config --global mergetool.p4merge.cmd "p4merge.exe "$BASE" "$LOCAL" "$REMOTE" "$MERGED""

变化(相对于 Charles Bailey):

  • 添加到全局 git 配置中,即对所有 git 项目有效,而不仅仅是当前项目
  • 自定义工具配置值位于mergetool.[tool].cmd"中,而不是merge.[tool].cmd"(傻瓜,花了一个小时来解决为什么 git 一直抱怨不存在的工具)
  • 为所有文件名添加双引号,以便合并工具仍然可以找到带有空格的文件(我在 Powershell 的 msys Git 中对此进行了测试)
  • 注意 Perforce 会默认将其安装目录添加到 PATH,因此无需在命令中指定 p4merge 的完整路径

下载:http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools

<小时>

编辑(2014 年 2 月)

正如 @Gregory Pakosz 所指出的,最新的 msys git 现在本地"支持 p4merge(在 1.8.5.2.msysgit.0 上测试).

您可以通过运行来显示支持的工具列表:

git mergetool --tool-help

您应该在 availablevalid 列表中看到 p4merge.如果没有,请更新您的 git.

如果p4merge被列为available,它在你的PATH中,你只需要设置merge.toolem>:

git config --global merge.tool p4merge

如果它被列为valid,除了merge.tool之外,你还必须定义mergetool.p4merge.path:

git config --global mergetool.p4merge.path c:/Users/my-login/AppData/Local/Perforce/p4merge.exe

  • 以上是为当前用户安装 p4merge 时的示例路径,不是系统范围的(不需要管理员权限或 UAC 提升)
  • 虽然~应该扩展到当前用户的主目录(所以理论上路径应该是~/AppData/Local/Perforce/p4merge.exe),但这并没有为我工作
  • 更好的是利用环境变量(例如 $LOCALAPPDATA/Perforce/p4merge.exe),git 似乎没有为路径扩展环境变量(如果你知道如何要使其正常工作,请让我知道或更新此答案)

I've tried msysGit and Git on Cygwin. Both work just fine in and of themselves and both run gitk and git-gui perfectly.

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.)

解决方案

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:

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 :

git config --global mergetool.p4merge.cmd "p4merge.exe "$BASE" "$LOCAL" "$REMOTE" "$MERGED""

The changes (relative to 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

Download: http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools


EDIT (Feb 2014)

As pointed out by @Gregory Pakosz, latest msys git now "natively" supports p4merge (tested on 1.8.5.2.msysgit.0).

You can display list of supported tools by running:

git mergetool --tool-help

You should see p4merge in either available or valid list. If not, please update your git.

If p4merge was listed as available, it is in your PATH and you only have to set merge.tool:

git config --global merge.tool p4merge

If it was listed as valid, you have to define mergetool.p4merge.path in addition to 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)

这篇关于Windows 上的 Git:如何设置合并工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆