How can I do a three way graphical diff (not merge) with git?

时光毁灭记忆、已成空白 提交于 2019-12-02 10:02:09

The manual one-liner command

You can invoke meld on three versions of the file without using temporary files by combining the <(cmd) syntax in bash with your git show idea:

meld <(git show master:file) <(git show branch1:file) <(git show branch2:file)

This will pop up meld with files dev/fd/61, /dev/fd/62 and /dev/fd/63 refering to the file in each of the three branches. The names are not very friendly, but you'll get used to that. The point is that it will show what you want to see.

Scripting it

The obvious next step is to simplify the syntax with a script:

Create file ~/bin/git-meld3 (or anywhere else on your PATH):

#!/bin/bash
meld <(git show $1:$4) <(git show $2:$4) <(git show $3:$4)

Make it executable:

chmod +x ~/bin/git-meld3

Call it:

git meld3 master branch1 branch2 myfilename

The command works with any committish:

git meld3 master 36d1cf756 HEAD^^^ myfilename

A more flexible script

This ~/bin/git-meld script accepts two or three committishes:

#!/bin/bash
if [[ $# -eq 3 ]]; then
   meld <(git show $1:$3) <(git show $2:$3)
elif [[ $# -eq 4 ]]; then
   meld <(git show $1:$4) <(git show $2:$4) <(git show $3:$4)
else
   echo Usage: git meld committish1 committish2 [committish3] file >&2
   exit 1
fi

PS

On my own machine, I have to invoke meld like this: python2.6 /usr/bin/meld, possibly because it's not installed correctly, so this is my actual ~/bin/git-meld3 script:

#!/bin/bash
python2.6 /usr/bin/meld <(git show $1:$4) <(git show $2:$4) <(git show $3:$4)

Meld is old! the script says it requires Python 2.4, but it fails to compile with 2.7 or 3. Fortunately, it does run with 2.6, so I was able to test my solution.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!