【问题标题】:Git on Windows: How do you set up a mergetool?Windows 上的 Git:如何设置合并工具?
【发布时间】:2010-09-30 08:59:00
【问题描述】:

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

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

【问题讨论】:

标签: windows git cygwin msysgit


【解决方案1】:

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

变化(相对于查尔斯·贝利):

  • 添加到全局 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.tool em>:

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 似乎没有为路径扩展环境变量(如果你知道如何让它工作,请告诉我或更新这个回答)

【讨论】:

  • 嗯 - 试试这个 - 第一个配置工作正常,下一个只显示 git config 的帮助文本。不知道为什么。
  • 知道了——那些单引号在我看来很可疑。只需将其翻倍,它们就会起作用。
  • 设置 mergetool.p4merge.cmd 将不再起作用,因为 Git 已经开始尝试支持 p4merge,请参阅 libexec/git-core/git-mergetool--lib 。而是直接使用mergetool.p4merge.path
  • 必须输入完整路径并转义双引号:cmd = \""c:/Program Files/TortoiseSVN/bin/TortoiseMerge.exe"\" -base:"$BASE" -theirs:"$REMOTE" -mine:"$LOCAL" -merged:"$MERGED"
  • 这是为我做的:git config --global mergetool.p4merge.cmd '"C:\\Program Files\\Perforce\\p4merge" $BASE $LOCAL $REMOTE $MERGED' 在 Windows 7 机器上
【解决方案2】:

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

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

然后就可以了。

【讨论】:

  • 也适用于 Beyond Compare 3。只需使用 BComp.exe 的路径即可。谢谢!
  • 我意识到这是一个旧线程,但我无法找到工作的路径。考虑到 cmd/msys 的相互作用,我对路径应该如何转义感到有点困惑...... EDIT 切换到双引号修复了它!
  • 谢谢 Gitninja !请注意,您可以在 Git Bash 中输入此内容,但如果您使用命令提示符,则必须将单引号更改为双引号
【解决方案3】:

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

但我在this blog post 中找到了让它在 Windows 中工作所需的说明,这些说明与此处列出的其他方法略有不同。它基本上相当于将这些行添加到我的.gitconfig 文件中:

[merge]
    tool = kdiff3

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

现在工作得很好!

【讨论】:

  • 在我的 Win7 PC 上,我必须使用 path = C:\\Program Files (x86)\\KDiff3\\kdiff3.exe(注意双反斜杠)
  • 我收到类似这样的错误“差异工具 meld 不能用作 'D:\software\melddiff\Meld.exe'”
  • 你知道每个部分添加的顺序是否有差异。我的意思是,如果 [merge] 在 [mergetool] 之后?
【解决方案4】:

在 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

【讨论】:

    【解决方案5】:

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

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

    简而言之,您可以通过设置用户配置变量merge.tool来设置默认合并工具。

    如果合并工具是它本机支持的工具之一,您只需将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 工具是一个非常好的独立合并工具。

    【讨论】:

      【解决方案6】:

      在 Windows 7 上无与伦比

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

      【讨论】:

      • 我使用了这些命令,但我使用的是 WinMerge 而不是 Beyond Compare。
      【解决方案7】:

      好像新的git版本直接支持p4merge,所以

      git config --global merge.tool p4merge
      

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

      【讨论】:

      • 这是关键。安装 p4merge 并将 mergetool.p4merge.path 设置为确切位置后,它仍然无法正常工作。将安装文件夹添加到路径确实有效。
      【解决方案8】:

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

      【讨论】:

        【解决方案9】:

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

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

        【讨论】:

          【解决方案10】:

          我在 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\"
          

          【讨论】:

            【解决方案11】:

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

            【讨论】:

              【解决方案12】:

              呸,这终于为我工作了(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 的提示!

              【讨论】:

                【解决方案13】:

                我使用一个名为 WinMerge (http://winmerge.org/) 的应用程序 他们手册中的信息(http://manual.winmerge.org/CommandLine.html

                这是我通过.gitconfig 使用mergetool 指令的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 的结果放在一个空文件中,并在正确的位置创建一个新的临时文件。

                【讨论】:

                  【解决方案14】:

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

                  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
                  

                  另外,我不知道为什么,但 Milan Gardian 的回答中的引用和斜线让我搞砸了。

                  【讨论】:

                    【解决方案15】:

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

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

                    【讨论】:

                      【解决方案16】:

                      适用于Windows环境下IntelliJ IDEA(社区版)3路git mergetool配置(~/.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 是在msys上将路径转换为Windows,取自msys path conversion question on stackoverflow

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

                      【讨论】:

                        【解决方案17】:

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

                        【讨论】:

                          【解决方案18】:

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

                          【讨论】:

                            【解决方案19】:

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

                            【讨论】:

                              猜你喜欢
                              • 2013-01-27
                              • 2010-10-23
                              • 2016-01-23
                              • 1970-01-01
                              • 1970-01-01
                              • 2018-10-06
                              • 2011-06-15
                              • 2015-10-22
                              相关资源
                              最近更新 更多