HOWTO · Git

为 Git 设置 SourceGear DiffMerge 工具

在本文中,我们将讨论配置 Git 以使用 SourceGear 的 DiffMerge 作为默认 difftool 的过程。

本页内容

本文将讨论配置 Git 以使用 SourceGear 的 DiffMerge 作为默认 difftool。与 Git 的默认 difftool 相比,此工具使我们能够通过改进的视觉效果轻松理解合并冲突。

它是 3 路合并的理想工具,因为它支持 3 路文件比较。

我们可以查看文件的先前状态、当前状态以及合并后的结果。你必须从 SourceGear 的官方网站下载并安装 DiffMerge

在 Windows 上设置 DiffMerge

要将 Git 设置为默认使用 DiffMerge 工具,请将其添加到你的 .gitconfig 文件中。

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

对于比较参数参数,我们有:

  1. $LOCAL,这是原始文件。
  2. $REMOTE,即修改后的文件。

你可以在 Git Bash 上运行 git difftoolgit mergetool 命令来打开 DiffMerge

或者,你可以在命令提示符下运行以下命令来更新 .gitconfig 文件。

C:\> git config --global diff.tool diffmerge
C:\> git config --global difftool.diffmerge.cmd
    "C:/Program\ Files/SourceGear/Common/DiffMerge/sgdm.exe
        \"$LOCAL\" \"$REMOTE\""

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

在 Mac OS 上设置 DiffMerge

在 Mac OS 上配置 Git 以使用 DiffMerge 的过程可能是最简单的。确保你有下面的目录。

/usr/local/bin/diffmerge

运行以下命令来更新你的 .gitconfig 文件并进行测试。

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "/usr/local/bin/diffmerge --merge --result=\"\$MERGED\" \"\$LOCAL\" \"\$BASE\" \"\$REMOTE\""
git config --global mergetool.diffmerge.trustExitCode true
git config --global mergetool.keepBackup false
git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/usr/local/bin/diffmerge --nosplash \"\$LOCAL\" \"\$REMOTE\""

我们可以运行 git config --global -e 命令来检查我们的 .gitconfig 文件。它应该看起来像这样:

[diff]
	tool = diffmerge

[merge]
	tool = diffmerge

...

[mergetool "diffmerge"]
	cmd = /usr/local/bin/diffmerge --merge --result=\"$MERGED\" \"$LOCAL\" \"$BASE\" \"$REMOTE\"
	trustExitCode = true
[mergetool]
	keepBackup = false
[difftool "diffmerge"]
	cmd = /usr/local/bin/diffmerge --nosplash \"$LOCAL\" \"$REMOTE\"

在 Ubuntu 上设置 DiffMerge

在 Ubuntu 上进行配置与 Mac OS 非常相似。我们将使用相同的命令,但更改安装位置。

以下是命令。

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "/usr/bin/diffmerge --merge --result=\"\$MERGED\" \"\$LOCAL\" \"\$BASE\" \"\$REMOTE\""
git config --global mergetool.diffmerge.trustExitCode true
git config --global mergetool.keepBackup false
git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "/usr/bin/diffmerge --nosplash \"\$LOCAL\" \"\$REMOTE\""

差不多就是这样。我们现在可以运行下面的命令来测试它。

$ git mergetool
$ git difftool

总而言之,如果你想配置 Git 以使用 SourceGear 的 DiffMerge 工具,你需要根据你的操作系统使用列出的命令更新你的 .gitconfig 文件。