轻轻的 发表于 2018-1-15 12:54:20

Git配置可视化的diff 和merge工具

  Windows下使用Git,msysgit是首选,但是msysgit的shell实在不给力,大小不能更改,字体难看。所以,在Windows下,在Cygwin下使用Git是个很不错的选择。
  我们在提交代码前,或是合并代码,查看代码修改时,经常要diff一下看看都有哪些修改内容,diff的输出,晦涩难懂,修改多了的时候,简直像天书一样。Git 1.7以后,有了一个difftool的命令,使用它,用户可以选择一个自己喜欢的diff工具来查看不同提交之间的差异。这个工具可以是命令行的如vimdiff,也可以是带GUI的如Winmerge等。
  Windows下的Diff工具有很多,WinMerge(免费), Araxis Merge(收费),装了TortoiseSVN的话,也带有一个Diff工具TortioseIDiff。
  这里推荐一款SourceGear MergeDiff,支持Windows,Mac,Linux,非常好用。

1. Diff
  首先,新建一个脚本 /usr/bin/mydiff.sh,使用以下内容
  

#!/bin/sh  

  
"$(cygpath -u "C:\Program Files\SourceGear\Common\DiffMerge\sgdm")" \
  
-caption="DiffMerge For Git" -nosplash $(cygpath -w $1) $(cygpath -w $2)
  

  然后在Git中使用我们的工具
  

# 设置执行权限  > chmod a+x /usr/bin/mydiff.sh
  # 添加一个diff工具
  
> git config --global diff.tool mydiff
  

  
# 配置mydiff的命令
  
> git config --global difftool.mydiff.cmd "mydiff.sh \"\$LOCAL\" \"\$REMOTE\""
  

  
# 查看当前目录的修改
  
> git difftool
  

  
# 使用-y,不必每次询问
  
> git difftool -y
  

  
# 查看两个版本之间的差异
  
> git difftool HEAD~2 HEAD
  


2. Merge
  同样的道理,我们也可以使用自定义的Merge工具
  首先,新建一个脚本 /usr/bin/mymerge.sh,使用以下内容
  

#!/bin/sh  

  
# path for DiffMerge
  
DMPATH="C:\Program Files\SourceGear\Common\DiffMerge\sgdm"
  

  
BASEPATH=$(cygpath -w -a "$1")
  
LOCALPATH=$(cygpath -w -a "$2")
  
REMOTEPATH=$(cygpath -w -a "$3")
  
RESULTPATH=$(cygpath -w -a "$4")
  

  
if [ ! -f $1 ]
  
then
  
echo "No Base, Use Empty"
  
TMPBASE="/tmp/git-empty-base"
  
touch $TMPBASE
  
BASEPATH=$(cygpath -w -a "$TMPBASE")
  
fi
  

  
# echo "Base: ""$BASEPATH"
  
# echo "Local: ""$LOCALPATH"
  
# echo "Remote: ""$REMOTEPATH"
  
# echo "Result: ""$RESULTPATH"
  

  
"$(cygpath -u "$DMPATH")" -caption="DiffMerge For Git" -nosplash \
  
-merge -result "$RESULTPATH" -t1=Mine -t2=Merged -t3=Theirs \
  
"$LOCALPATH" "$BASEPATH" "$REMOTEPATH"
  

  然后设置使用mymerge.sh
  

> git config --global merge.tool mymerge  
> git config --global mergetool.mymerge.cmd "mymerge.sh \"\$BASE\" \"\$LOCAL\" \"\$REMOTE\" \"\$MERGED\""
  
> git config --global mergetool.mymerge.trustExitCode false
  

  
# 当merge和rebase发生冲突的时候
  
> git mergetool
  


3. Color
  msysgit中使用git status时会发现是带颜色输出,看起来很直观,其实设置一下就可以了。
  

> git config --global color.diff auto  
> git config --global color.status auto
  
> git config --global color.branch auto
  
> git config --global color.interactive true
  

  或者直接修改~/.gitconfig
  

  
tool = mydiff
  

  
cmd = mydiff.sh \"$LOCAL\" \"$REMOTE\"
  

  
diff = auto
  
status = auto
  
branch = auto
  
interactive = true
  

  
st = status
  
lg = log -p
  
lol = log --graph --decorate --pretty=oneline --abbrev-commit
  
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
  
ls = ls-files
  

  
autocrlf = true
  

  
tool = mymerge
  

  
cmd = mymerge.sh \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
  
trustExitCode = false
  

  这时再看log,直观很多
https://images0.cnblogs.com/blog/165220/201212/20144301-88237521543b4139a8b6555a78b96006.PNG
页: [1]
查看完整版本: Git配置可视化的diff 和merge工具