在Windows上的Git:你如何设置一个mergetool?

我已经尝试了Cygwin上的msysGit和Git。 两者都可以正常工作,完美地运行gitk和git-gui。

现在,我怎么configuration一个mergetool? (Vimdiff在Cygwin上工作,但是最好我喜欢一些对我们的Windows爱好者同事更友好的东西。)

为了对Charles Bailey的答案进行跟进,这里是我使用p4merge (免费的跨平台3way合并工具)的git设置; 在msys Git(Windows)上testing安装:

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

或者,从一个Windows cmd.exeshell,第二行变成:

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

变化(相对于Charles Bailey):

  • 添加到全球gitconfiguration,即有效的所有git项目不只是目前的一个
  • 自定义工具configuration值驻留在“mergetool。[tool] .cmd”,而不是“merge。[tool] .cmd”(愚蠢的我,花了一个小时解决了为什么git一直抱怨不存在的工具)
  • 为所有文件名添加双引号,这样空格的文件仍然可以通过合并工具find(我在Powershell的msys Git中testing过)
  • 请注意,默认情况下,Perforce会将其安装目录添加到PATH,因此不需要在命令中指定p4merge的完整path

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


编辑 (2014年2月)

正如@Gregory Pakosz指出的那样,最新的msys git现在“本地”支持p4merge (在1.8.5.2.msysgit.0testing )。

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

 git mergetool --tool-help 

您应该在可用有效的列表中看到p4merge 。 如果没有,请更新你的git。

如果p4merge被列为可用 ,它在你的PATH ,你只需要设置merge.tool

 git config --global merge.tool p4merge 

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

 git config --global mergetool.p4merge.path c:/Users/my-login/AppData/Local/Perforce/p4merge.exe 
  • 以上是为当前用户安装p4merge时的示例path,而不是系统范围(不需要pipe理员权限或UAC提升)
  • 虽然~应扩展到当前用户的主目录(所以理论上path应该是~/AppData/Local/Perforce/p4merge.exe ),但这对我并不适用
  • 更好的办法是利用一个环境variables(例如$LOCALAPPDATA/Perforce/p4merge.exe ),git似乎没有扩展环境variables的path(如果你知道如何得到这个工作,请让我知道或更新这个答案)

设置mergetool.p4merge.cmd将不再工作,因为Git已经开始尝试支持p4merge,请参阅libexec / git-core / git-mergetool–lib.so我们只需要指定git的mergetoolpath,例如p4merge:

 git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe' git config --global merge.tool p4merge 

然后它会工作。

我在WinXP上使用了便携式Git(工作一种享受!),并需要解决分支中出现的冲突。 在我检查的所有gui中, KDiff3被certificate是最透明的。

但是我在这篇博客文章中find了我需要在Windows中工作的说明,这些说明与此处列出的其他方法略有不同。 基本上.gitconfig于将这些行添加到我的.gitconfig文件中:

 [merge] tool = kdiff3 [mergetool "kdiff3"] path = C:/YourPathToBinaryHere/KDiff3/kdiff3.exe keepBackup = false trustExitCode = false 

现在工作很好!

在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)"' 

另外,我喜欢closuresdifftool的提示信息:

 git config --global difftool.prompt false 

git mergetool是完全可configuration的,所以你几乎可以select你喜欢的工具。

完整的文档在这里: http : //www.kernel.org/pub/software/scm/git/docs/git-mergetool.html

简而言之,您可以通过设置用户configurationvariablesmerge.tool来设置默认的mergetool。

如果合并工具是本地支持的合并工具之一,则只需将merge tool mergetool.<tool>.path设置为该mergetool.<tool>.path的完整path(将<tool>replace为您configuration了mergetool.<tool>.path的工具即可)。

否则,您可以将mergetool.<tool>.cmd设置为一些shell,以便在运行时将shellvariables$BASE, $LOCAL, $REMOTE, $MERGED设置为相应的文件。 不pipe你是直接编辑一个configuration文件,还是使用git config命令设置variables,你都必须注意转义。

像这样的东西应该给你可以做的味道('mymerge'是一个虚构的工具)。

 git config merge.tool mymerge git config merge.mymerge.cmd 'mymerge.exe --base "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"' 

一旦你设置了你最喜欢的合并工具,只要你有解决冲突的时候运行git mergetool

Perforce的p4merge工具是一个相当不错的独立合并工具。

看来,新的git版本直接支持p4merge,所以

 git config --global merge.tool p4merge 

应该是所有你需要的,如果p4merge.exe是你的path。 无需设置cmd或path。

在Windows 7上无法比拟

 git config --global merge.tool bc3 git config --global mergetool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\BCompare.exe" 

正如已经在这里(以及在这里和这里 )回答的那样,mergetool是configuration这个的命令。 对于一个不错的graphics前端,我推荐kdiff3 (GPL)。

我不得不在Windows 7上使用msysGit删除额外的引用,不知道为什么。

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

如果你通过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`' 

巴,这终于为我工作(Windows 7 + Cygwin + TortoiseMerge):

在.git / config中:

 cmd = TortoiseMerge.exe /base:$(cygpath -d \"$BASE\") /theirs:$(cygpath -d \"$REMOTE\") /mine:$(cygpath -d \"$LOCAL\") /merged:$(cygpath -d \"$MERGED\") 

感谢以前的海报提示使用cygpath!

我从他们的手册( http://manual.winmerge.org/CommandLine.html )使用一个名为WinMerge( http://winmerge.org/

这是我通过.gitconfigmergetool指令中使用的bash脚本

 #!/bin/sh # using winmerge with git # replaces unix style null files with a newly created empty windows temp file file1=$1 if [ "$file1" == '/dev/null' ] || [ "$file1" == '\\.\nul' ] || [ ! -e "$file1" ] then file1="/tmp/gitnull" `echo "">$file1` fi file2=$2 if [ "$file2" == '/dev/null' ] || [ "$file2" == '\\.\nul' ] || [ ! -e "$file2" ] then file2="/tmp/gitnull" `echo "">$file2` fi echo diff : $1 -- $2 "C:\Program files (x86)\WinMerge\WinMergeU.exe" -e -ub -dl "Base" -dr "Mine" "$file1" "$file2" 

基本上这个bash是在一个空文件中比较diff的结果并在正确的位置创build一个新的临时文件。

您可能还想添加这些选项:

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

另外,我不知道为什么,但是从米兰加迪安的回答引用和斜杠搞砸了我的东西。

如果有人想在gortimgit上使用gvim作为它们的diff工具,那么这就是你需要input到外部diff工具path的文本input中的东西:

 path\to\gvim.exe -f -d -c "wincmd R" -c "wincmd R" -c "wincmd h" -c "wincmd J" 

对于Windows环境中的IntelliJ IDEA(Community Edition)3路git mergetoolconfiguration( ~/.gitconfig

Cygwin的

 [mergetool "ideamerge"] cmd = C:/Program\\ Files\\ \\(x86\\)/JetBrains/IntelliJ\\ IDEA\\ Community\\ Edition\\ 14.1.3/bin/idea.exe merge `cygpath -wa $LOCAL` `cygpath -wa $REMOTE` `cygpath -wa $BASE` `cygpath -wa $MERGED` [merge] tool = ideamerge 

MSYS

 [mergetool "ideamerge"] cmd = "/c/Program\\ Files\\ \\(x86\\)/JetBrains/IntelliJ\\ IDEA\\ Community\\ Edition\\ 14.1.3/bin/idea.exe" merge `~/winpath.sh $LOCAL` `~/winpath.sh $REMOTE` `~/winpath.sh $BASE` `~/winpath.sh $MERGED` [merge] tool = ideamerge 

〜/ winpath.sh是将path转换为msys上的Windows,并取自msys在stackoverflow上的path转换问题

 #! /bin/sh function wpath { if [ -z "$1" ]; then echo "$@" else if [ -f "$1" ]; then local dir=$(dirname "$1") local fn=$(basename "$1") echo "$(cd "$dir"; echo "$(pwd -W)/$fn")" | sed 's|/|\\|g'; else if [ -d "$1" ]; then echo "$(cd "$1"; pwd -W)" | sed 's|/|\\|g'; else echo "$1" | sed 's|^/\(.\)/|\1:\\|g; s|/|\\|g'; fi fi fi } wpath "$@" 

我发现有两种方法在github Windows中将“ SourceGear DiffMerge ”configuration为difftool和mergetool。

命令提示符窗口中的以下命令将更新您的.gitconfig以configurationGIT使用DiffMerge:

 git config --global diff.tool diffmerge git config --global difftool.diffmerge.cmd 'C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe \"$LOCAL\" \"$REMOTE\"' git config --global merge.tool diffmerge git config --global mergetool.diffmerge.cmd 'C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe -merge -result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\"' 

[ ]

将下列行添加到.gitconfig中。 这个文件应该在C:\ Users \ UserName的主目录下:

 [diff] tool = diffmerge [difftool "diffmerge"] cmd = C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe \"$LOCAL\" \"$REMOTE\" [merge] tool = diffmerge [mergetool "diffmerge"] trustExitCode = true cmd = C:/Program\\ Files/SourceGear/Common/DiffMerge/sgdm.exe -merge -result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\" 

要设置p4merge,使用巧克力在windows上安装merge和diff,请看看这里: https ://gist.github.com/CamW/88e95ea8d9f0786d746a

如果您在从SourceTree打开p4merge时遇到问题,请在MyRepo.git下查找名为config的本地configuration文件,并删除所有合并configuration。 在我的情况下,它试图打开我刚刚卸载的Meld