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 檔案。