无法在 Git 中的两个独立分支中比较文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/916822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:30:48  来源:igfitidea点击:

Unable to diff files in two separate branches in Git

gitunixmergediff

提问by Léo Léopold Hertz ??

I have FileA in branchA and FileB in branchB.

我在 branchA 中有 FileA,在 branchB 中有 FileB。

The problem is that I can access only one file at time. I would like to be able to compare the files by FileMerge or meld, since they are the only diffTools whichI have found for Mac.

问题是我一次只能访问一个文件。我希望能够通过 FileMerge 或 meld 比较文件,因为它们是我为 Mac 找到的唯一 diffTools。

How can you diff by meld/FileMerge the two files?

如何通过 meld/FileMerge 区分这两个文件



[Solved]: 1st developed Problem:FileMerge does not allow standard input

[已解决]:第一个开发问题:FileMerge 不允许标准输入

Masi: You can use opendiff to allow FileMergeto have files from standard input. So the next problem is to find how to make git's diff tool to use opendiff.

Masi:您可以使用opendiff 来允许 FileMerge拥有来自标准输入的文件。所以接下来的问题是要找到如何让git的diff工具使用opendiff。



2nd developed Problem:to make Git's diff tool to use opendiff in Mac

第二个开发问题:使 Git 的 diff 工具在 Mac 中使用 opendiff

回答by Jakub Nar?bski

You can use "git mergetool" for merging, and in modern git (meaning version 1.6.3 and later) "git difftool" for comparing using graphical tools. Of course you would have to configure them first, but they do some autodetection (with some hardcoded preference, of course), and if I remember correctly opendiff support is built in.

您可以使用“ git mergetool”进行合并,并在现代git(意味着1.6.3及更高版本)中使用“ git difftool”来使用图形工具进行比较。当然,您必须先配置它们,但它们会进行一些自动检测(当然,有一些硬编码的偏好),如果我没记错的话,内置了 opendiff 支持。

And then of course you would be able to use your graphical tool (opendiff / FileMerge) as you would use ordinary "git diff", for example

然后当然你可以像使用普通的“git diff”一样使用你的图形工具(opendiff / FileMerge),例如

prompt> git difftool somebranch:UNREADME otherbranch:README

回答by Kyle Burton

git supports branch names as part of the repository paths. Eg if you have the following files in your repository, READMEonly on master, and UNREADMEonly on branch:

git 支持将分支名称作为存储库路径的一部分。例如,如果您的存储库中有以下文件,则README仅在masterUNREADMEbranch

master:README

branch:UNREADME

You can diff them via git with:

你可以通过 git 来区分它们:

git diff branch:UNREADME master:README

You can get a repository artifact to standard output with git show:

您可以使用以下命令将存储库工件发送到标准输出git show

git show branch1:UNREADME

So if your external diff utility can take 2 files on the bash prompt, you can diff them with something like:

因此,如果您的外部 diff 实用程序可以在 bash 提示符下接收 2 个文件,您可以使用以下内容对它们进行比较:

diff-command <(git show branch1:UNREADME) <(git show master:README)

Where the <(...)bash syntax takes the output of the enclosed command, runs it in a pipe and places the file path of the pipe on the command line.

其中<(...)bash 语法采用封闭命令的输出,在管道中运行它并将管道的文件路径放在命令行上。

回答by Jarret Hardie

If you're currently checkout out to branchA, for example, you can use the command:

例如,如果您当前正在结帐到 branchA,则可以使用以下命令:

git diff branchB path/to/common/file/between/branches.txt

You can then edit the diff if you want a subset of the changes, or leave it as is, and git applythe diff as a patch. As for a GUI tool that does this, hopefully someone else will have a suggestion there.

如果您想要更改的子集,则可以编辑差异,或者保持原样,git apply差异作为补丁。至于执行此操作的 GUI 工具,希望其他人会在那里提出建议。

回答by Hank Gay

From the docs, you need something similar to the following in your .git/config file:

docs 中,您需要在 .git/config 文件中包含类似于以下内容的内容:

# Our diff algorithm
[diff]
    external = opendiff