繁体   English   中英

Windows 上的 Git:如何设置合并工具?

[英]Git on Windows: How do you set up a mergetool?

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

现在我该如何配置合并工具? (Vimdiff 在 Cygwin 上工作,但最好我想要一些对我们一些喜欢 Windows 的同事更用户友好的东西。)

为了跟进 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\"'

或者,从 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

您应该在可用有效列表中看到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 时的示例路径,不是系统范围的(不需要管理员权限或 UAC 提升)
  • 虽然~应该扩展到当前用户的主目录(所以理论上路径应该是~/AppData/Local/Perforce/p4merge.exe ),但这对我不起作用
  • 更好的是利用环境变量(例如$LOCALAPPDATA/Perforce/p4merge.exe ),git 似乎没有扩展路径的环境变量(如果您知道如何使其工作,请告诉我或更新这个答案)

由于 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

然后它会起作用。

我在 WinXP 上使用便携式 Git(很好用!),并且需要解决分支中出现的冲突。 在我检查过的所有 gui 中, KDiff3被证明是最透明的。

但是我在这篇博文中找到了让它在 Windows 中运行所需的说明,这些说明与此处列出的其他方法略有不同。 它基本上相当于将这些行添加到我的.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)"'

另外,我喜欢关闭difftool的提示信息:

git config --global difftool.prompt false

git mergetool是完全可配置的,因此您几乎可以选择自己喜欢的工具。

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

简单地说,你可以通过设置用户配置变量设置默认的合并工具merge.tool

如果合并工具是它本机支持的工具之一,您只需将mergetool.<tool>.path设置为mergetool.<tool>.path的完整路径(将<tool>替换为您配置的merge.tool

否则,您可以将mergetool.<tool>.cmd设置为在运行时进行评估的一些 shell,并将 shell 变量$BASE, $LOCAL, $REMOTE, $MERGED设置为适当的文件。 无论是直接编辑配置文件还是使用git config命令设置变量,都必须小心转义。

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

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

一旦你设置了你最喜欢的合并工具,只要你有冲突需要解决,只要运行git mergetool就可以了。

Perforce 的 p4merge 工具是一个非常好的独立合并工具。

无法在 Windows 7 上进行比较

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

似乎较新的 git 版本直接支持 p4merge,所以

git config --global merge.tool p4merge

如果 p4merge.exe 在您的路径上,应该就是您所需要的。 无需设置 cmd 或路径。

正如这里(以及这里这里)已经回答的那样,mergetool 是配置它的命令。 对于一个不错的图形前端,我推荐kdiff3 (GPL)。

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

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

我找到了两种在 github Windows 中将“ SourceGear DiffMerge ”配置为 difftool 和 mergetool 的方法。

命令提示符窗口中的以下命令将更新您的 .gitconfig 以配置 GIT 使用 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\"

我使用了一个名为 WinMerge ( http://winmerge.org/ ) 的应用程序,来自他们的手册 ( http://manual.winmerge.org/CommandLine.html )

这是我通过.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 的结果何时出现在一个空文件中,并在正确的位置创建了一个新的临时文件。

如果您通过 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 的提示!

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

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

另外,我不知道为什么,但是米兰加迪安的回答中的引用和斜线让我把事情搞砸了。

如果有人想在 TortoiseGit 上使用 gvim 作为他们的差异工具,那么您需要在文本输入中输入外部差异工具的路径:

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

Windows环境下IntelliJ IDEA(社区版)3路git mergetool配置( ~/.gitconfig

赛格温

[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

管理系统

[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 用于将路径转换为 ​​msys 上的 Windows,取自stackoverflow 上的 msys 路径转换问题

#! /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 "$@" 

要设置 p4merge,在 Windows 上使用 Chocolatey 安装用于合并和差异,请查看此处: https ://gist.github.com/CamW/88e95ea8d9f0786d746a

如果您在从 SourceTree 打开 p4merge 时遇到问题,请在 MyRepo.git 下查找名为config 的本地配置文件并删除任何合并配置。 就我而言,它试图打开我刚刚卸载的 Meld

对于 kdiff3,您可以使用:

git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

git config --global diff.guitool kdiff3
git config --global difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global difftool.kdiff3.trustExitCode false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM