Git配置merge工具

Git Merge的常用工具有kdiff3和p4Merge。
下面就Windows系统和OS X系统分别说明如果配置这两种工具。

OS X系统


1、创建一个merge包装脚本,名字叫作extMerge,让它带参数调用p4merge二进制文件

$ sudo vim /usr/local/bin/extMerge

内容为

#!/bin/sh
/Applications/p4merge.app/Contents/MacOS/p4merge $*

2、接着创建diff包装脚本

$ sudo vim /usr/local/bin/extDiff

内容为

#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/extMerge "$2" "$5"

3、确认这两个脚本是可执行的

$ sudo chmod +x /usr/local/bin/extMerge
$ sudo chmod +x /usr/local/bin/extDiff

4、配置~/.gitconfig

$ git config --global merge.tool extMerge
$ git config --global mergetool.extMerge.cmd \
'extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"'
$ git config --global mergetool.trustExitCode false
$ git config --global diff.external extDiff

或者直接编辑~/.gitconfig文件如下

[merge]
  tool = extMerge
[mergetool "extMerge"]
  cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
  trustExitCode = false
[diff]
  external = extDiff

如果想替换kdiff3作为默认的merge工具,则修改extMerge脚本,把p4Merge替换成kdiff3

$ sudo vim /usr/local/bin/extMerge

内容为

#!/bin/sh
/Applications/kdiff3.app/Contents/MacOS/kdiff3 $*

5、配置好了之后就可以通过命令行调用工具了。以diff为例

▼一个一个的diff

git diff
git difftool

▼一起diff(kdiff3可以,p4Merge不可以)

git difftool --dir-diff
git difftool -d


Windows系统


git config --global diff.tool p4merge
git config --global difftool.p4merge.cmd "'D:\Program Files\Perforce\p4merge.exe' $LOCAL $REMOTE"

git config --global merge.tool p4merge
git config --global mergetool.p4merge.cmd "'D:\Program Files\Perforce\p4merge.exe' $PWD/$BASE $PWD/$REMOTE $PWD/$LOCAL $PWD/$MERGED"
git config --global mergetool.p4merge.trustExitCode false
git config --global mergetool.keepBackup false

同样,如果想要以kdiff3作为默认的merge工具,可以添加

difftool.kdiff3.cmd
mergetool.kdiff3.cmd
mergetool.kdiff3.trustExitCode false

并把

diff.tool
merge.tool

替换为kdiff3

你可能感兴趣的:(Git配置merge工具)