我在 Cygwin 上尝试过 msysGit 和 Git。两者本身都工作得很好,并且都完美地运行 gitk 和 git-gui。

现在我到底该如何配置合并工具呢?(Vimdiff 可以在 Cygwin 上运行,但我希望对于一些喜欢 Windows 的同事来说,它能更加用户友好。)

有帮助吗?

解决方案

为了跟进 Charles Bailey 的答案,这是我正在使用的 git 设置 p4合并 (免费的跨平台3way合并工具);在 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 月)

正如所指出的 @格雷戈里·帕科斯, , 最新的 git 系统 现在“原生”支持 p4合并 (测试于 1.8.5.2.msysgit.0).

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

git mergetool --tool-help

你应该看到 p4合并 在任一 可用的 或者 有效的 列表。如果没有,请更新你的 git。

如果 p4合并 被列为 可用的, ,它在你的 小路 你只需要设置 合并工具:

git config --global merge.tool p4merge

如果它被列为 有效的, ,你必须定义 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 似乎没有扩展路径的环境变量(如果您知道如何使其工作,请让我知道或更新此答案)

其他提示

设置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

然后它会起作用。

我在WinXP上使用Portable 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设置为该工具的完整路径(将<tool>替换为您已配置的mergetool.<tool>.cmd)。

否则,您可以将$BASE, $LOCAL, $REMOTE, $MERGED设置为在运行时使用shell变量git config设置为相应文件的eval'。无论是直接编辑配置文件还是使用<=>命令设置变量,都必须小心转义。

这样的事情应该能够提供你能做的事情('mymerge'是一个虚构的工具)。

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

一旦您设置了自己喜欢的合并工具,只需要在遇到要解决的冲突时运行<=>。

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或路径。

正如此处已经回答的那样(以及此处和< a href =“https://stackoverflow.com/questions/161813/how-do-i-fix-merge-conflicts-in-git”>这里),mergetool是配置它的命令。对于一个漂亮的图形前端,我建议 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`'

Bah,这个终于为我工作了(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!

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

这是我通过mergetool

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

我找到了两种方法来配置<!>“; SourceGear DiffMerge <!>”;在github Windows中作为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\"

您可能也想要添加这些选项:

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"

对于IntelliJ IDEA(Community Edition)Windows环境中的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,并取自 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安装merge和diff,请看一下: https://gist.github.com/CamW/88e95ea8d9f0786d746a

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top