git 如何在github中的两个分支之间为单个文件生成差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14500240/
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
How can I generate a diff for a single file between two branches in github
提问by plainflavour
I need to generate a diff for a single file that will show the differences between two versions, which are actually tags in github. I then want to send this diff to someone via email so a github URL for the diff would be ideal. The github compare view will allow me to do this for all changed files, but that's no good as there are thousands of files in my repo.
我需要为单个文件生成一个差异,该差异将显示两个版本之间的差异,这实际上是 github 中的标签。然后我想通过电子邮件将此差异发送给某人,因此差异的 github URL 将是理想的选择。github 比较视图将允许我对所有更改的文件执行此操作,但这并不好,因为我的存储库中有数千个文件。
I can do this in the command line as follows, but this doesn't help as I need to send the diff to someone via email:
我可以按如下方式在命令行中执行此操作,但这无济于事,因为我需要通过电子邮件将差异发送给某人:
git diff tag1 tag2 -- path/to/file
I found the command line version discussed here: how can I see the differences in a designated file between a local branch and a remote branch?
我找到了这里讨论的命令行版本: 如何查看本地分支和远程分支之间指定文件的差异?
采纳答案by nulltoken
GitHub only exposes the way to show diff between two commits.
GitHub 只公开了显示两次提交之间差异的方式。
Provided those tags actually point to commits, the Url format would be something like
如果这些标签实际上指向提交,则 Url 格式将类似于
https://github.com/{user}/{repository}/compare/{from-tag}...{until-tag}
https://github.com/{user}/{repository}/compare/{from-tag}...{until-tag}
As an example, https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5shows the diff between two versions of the LibGit2Sharp project. This diff includes all the modified files.
例如,https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5显示了 LibGit2Sharp 项目的两个版本之间的差异。此差异包括所有修改过的文件。
If you want to retrieve an url that targets a specific file:
如果要检索针对特定文件的 url:
- Switch to the Files Changedtab
- 切换到文件更改选项卡
- Click on the Show Diff Statsbutton (This will display the list of modified files as links)
- 单击Show Diff Stats按钮(这将显示修改后的文件列表作为链接)
- Copy to the clipboard the link of the specific file you're after... and Tada! You're done.
- 将您要查找的特定文件的链接复制到剪贴板……还有 Tada!你完成了。
For instance, given the diff above, the link https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5#diff-11will point to the LazyFixtures.cs
changes that occured between version v0.9.0 and v0.9.5.
例如,鉴于上面的差异,链接https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5#diff-11将指向LazyFixtures.cs
版本 v0.9.0 和版本之间发生的更改v0.9.5。
Update
更新
Following your comment which states that your diff is too big to be rendered through the Web interface, how about reverting to good old command line tooling? You could redirect the output of the diff to a file and then send the file as an email attachment.
根据您的评论指出您的差异太大而无法通过 Web 界面呈现,如何恢复到良好的旧命令行工具?您可以将 diff 的输出重定向到一个文件,然后将该文件作为电子邮件附件发送。
$ git diff v0.9.0 v0.9.5 -- LibGit2Sharp.Tests/LazyFixture.cs > /tmp/lazyfixture.diff
回答by f01
Here is my workaround when the following issue applies.
当以下问题适用时,这是我的解决方法。
This comparison is big! We're only showing the most recent 250 commits
这个比较大!我们只显示最近的 250 次提交
Copy the raw view of the file that you want to compare to https://gist.github.com/. Use the two specific commit points that you want to compare. Start with the older commit.
复制要与https://gist.github.com/进行比较的文件的原始视图。使用要比较的两个特定提交点。从较旧的提交开始。
https://gist.github.com/has a nice side-by-side diff view when you click 'Revisions'.
当您单击“修订”时,https://gist.github.com/有一个很好的并排差异视图。
回答by Kanagavelu Sugumar
Answer is for people who want to only see (Not to download) the history/reviosion of code changes of a file in the GITHUB WEB Page for previous checkin.
答案适用于那些只想在 GITHUB WEB 页面中查看(不下载)文件的代码更改历史/修订以进行先前签入的人。
Go to that file in the github, then select HISTORY. This will open page with list of checkin comments link like below.
转到 github 中的该文件,然后选择HISTORY。这将打开带有签入评论链接列表的页面,如下所示。
On clicking on it will show the code changes. After clicking the history; you can click on packages to see package level all files checkins.
单击它会显示代码更改。点击历史后;您可以单击包以查看包级别的所有文件签入。
In eclipse you can compare the history using EGitplugin and "Right click ->Compare with" on the file. How can I compare two revisions in git in Eclipse?
在 Eclipse 中,您可以使用EGit插件和文件上的“右键单击 -> 比较”来比较历史记录。 如何在 Eclipse 中比较 git 中的两个修订版?
回答by Chris
I used nulltoken's answerto put together a simple convenience script for pulling up a diff between two commits on GitHub from the command line.
我使用nulltoken 的答案组合了一个简单的便捷脚本,用于从命令行提取 GitHub 上两次提交之间的差异。
You can find the full script on gist, but here are the good bits:
# Parse the following patterns for repo urls to get the github repo url
# https://github.com/owner/repo-name.git
# [email protected]:owner/repo-name.git
BASE_URL="https://github.com/""$(git config --get remote.origin.url | sed 's/.*github\.com[/:]\(.*\).git//')""/compare"
if [[ "$#" -eq 1 ]]; then
if [[ "" =~ .*\.\..* ]]; then
# Handle "git hubdiff fromcommit..tocommit"
open "${BASE_URL}/$(git rev-parse "${1/\.\.*/}")...$(git rev-parse ${1/*\.\./})"
else
# Handle "git hubdiff fromcommit"
open "${BASE_URL}/$(git rev-parse "")...$(git rev-parse HEAD)"
fi
elif [[ "$#" -eq 2 ]]; then
# Handle "git hubdiff fromcommit tocommit"
open "${BASE_URL}/$(git rev-parse "")...$(git rev-parse "")"
fi
It accepts as arguments branches, commits, and anything else that can be resolved by git rev-parse
. I used open
, which only works on macOS for opening webpages, so if you're on a different environment you'll want to tweak that.
它接受分支、提交和任何其他可以由git rev-parse
. 我使用了open
,它只能在 macOS 上打开网页,所以如果你在不同的环境中,你会想要调整它。
As with nulltoken's answer, in order to point to a single file in the diff, you'll have to click on the file's title to make the anchor string appear in the url bar, which you can then copy.
与 nulltoken 的答案一样,为了指向差异中的单个文件,您必须单击文件的标题以使锚字符串出现在 url 栏中,然后您可以复制它。
回答by kayleeFrye_onDeck
Since this still isn't possible here's a browser-based diff-tool method. It does not leverage automation but only requires the ability to install Chrome extensions:
由于这仍然是不可能的,这里有一个基于浏览器的差异工具方法。它不利用自动化,而只需要能够安装 Chrome 扩展程序:
- Install Diff Tools for Chrome browsers: https://chrome.google.com/webstore/detail/diff-tools-text-pdf-doc-o/lkcdojpmjehlniamnglpjlldkoonlomb
- Open Diff Tools ( http://iblogbox.com/devtools/diff/)
- From GitHub go to the BEFORE commit, tag, or branch, open the file, then click on the Rawbutton to get the raw file view, select-all and copy, then put in the left-hand side text box in Diff Tools
- Repeat step 3 but for the AFTER file and paste into the right-hand-side box in Diff Tools
- Click Compare Nowand perform your diff ad-hoc
- 为 Chrome 浏览器安装 Diff 工具:https: //chrome.google.com/webstore/detail/diff-tools-text-pdf-doc-o/lkcdojpmjehlniamnglpjlldkoonlomb
- 打开 Diff 工具 ( http://iblogbox.com/devtools/diff/)
- 从 GitHub 转到 BEFORE 提交、标记或分支,打开文件,然后单击Raw按钮获取原始文件视图,全选并复制,然后放入 Diff 工具中的左侧文本框
- 重复步骤 3 但对于 AFTER 文件并粘贴到 Diff Tools 中的右侧框中
- 单击Compare Now并执行您的 diff ad-hoc