如何使用git做三向图形化差异(不合并)? [英] How can I do a three way graphical diff (not merge) with git?

查看:108
本文介绍了如何使用git做三向图形化差异(不合并)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git中有两个相似的分支,我想手动将这些更改拉到完全不同的第三个分支中.可以对这些更改进行三种方式的图形化比较吗?

I have two branches in git that are similar and I want to manually pull those changes into a third branch that is quite different. Is it possible to do a three way graphical diff of those changes?

当前已设置两个分支的图形差异,并且可以很好地与"meld"配合使用.以下显示了预期的图形化融合窗口:

Graphical diff of two branches is currently setup and working well with 'meld'. The following displays a graphical meld window as expected:

  • git diff branch1 branch2-我的文件名

我尝试执行以下操作,但是得到了'diff --cc'的ASCII输出.

I tried to do the following, but I get the ASCII output of 'diff --cc'.

  • git diff master branch1 branch2-myfilename
  • git diff master branch1 branch2 -- myfilename

这是获取三向差异的有效语法吗?什么是正确的.gitconfig设置,才能将其打开?我正在使用Git版本1.8.2.1.

Is that a valid syntax to get a 3-way diff? What is the proper .gitconfig settings to have it open meld? I'm using Git version 1.8.2.1.

我的期望是在一个融合窗口中打开这三个文件,然后我可以查看branch1/branch2之间的更改,然后从视觉上确保相同的更改在master中.我刚刚意识到,可以通过从三个分支中分别检出每个文件,然后将文件名作为参数直接传递给meld来做到这一点. 可以按照以下SO答案进行结帐: https://stackoverflow.com/a/888623/350265

My expectation is to have the three files opened in a meld window, then I can look at the changes between branch1/branch2 and then visually make sure the same change is in master. I just realized that I can do this by checking out each file from the three branches independently, then passing the filenames as arguments directly to meld. The checkout can be done as per this SO answer: https://stackoverflow.com/a/888623/350265

推荐答案

一线手动命令

通过将bash中的<(cmd)语法与git show想法结合在一起,可以在文件的三个版本上调用meld而不使用临时文件:

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)

这将弹出融合文件,分别引用三个分支中的文件dev/fd/61/dev/fd/62/dev/fd/63.名称不是很友好,但是您会习惯的.关键是它将显示您想要看到的内容.

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.

编写脚本

显而易见的下一步是使用脚本简化语法:

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

创建文件~/bin/git-meld3(或PATH中的其他任何位置):

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)

使其可执行:

chmod +x ~/bin/git-meld3

调用:

git meld3 master branch1 branch2 myfilename

该命令适用于任何内容:

The command works with any committish:

git meld3 master 36d1cf756 HEAD^^^ myfilename

更灵活的脚本

~/bin/git-meld脚本接受两个或三个承诺:

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

在我自己的机器上,我必须像这样调用Meld:python2.6 /usr/bin/meld,可能是因为未正确安装它,所以这是我的实际~/bin/git-meld3脚本:

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)

产地已老!该脚本说它需要Python 2.4,但是不能用2.7或3编译.所幸,它确实可以用2.6运行,因此我能够测试我的解决方案.

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.

这篇关于如何使用git做三向图形化差异(不合并)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆