git 在git中生成特定提交的差异文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42357521/
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-19 12:37:44  来源:igfitidea点击:

Generate diff file of a specific commit in git

gitgit-diff

提问by Ginu Jacob

When the head is at a particular commit, I want to get a diff file so that I can reduce the head to one more level down and then try the testing functionality with and without applying the diff file. So is there a way to generate a diff file of a specific commit.

当 head 处于特定提交时,我想获得一个 diff 文件,以便我可以将 head 降低到一个更高的级别,然后在应用和不应用 diff 文件的情况下尝试测试功能。那么有没有办法生成特定提交的差异文件。

Even though there is a way to change the head before and after commit, this method comes more handy.

尽管有一种方法可以在提交前后更改头部,但这种方法更方便。

回答by Sajib Khan

See the changes of a specific commit.

查看特定提交的更改。

$ git diff <commit-sha> -p

OR,
$ git show --decorate <commit-sha>    # see 'Author', 'Date' and 'diff'

See the diff of two commits.

查看两次提交的差异。

$ git diff <commit1> <commit2>

See the filechanges for a specific commit.

查看file特定提交的更改。

$ git show <commit>:<file>

See all the changes for a time duration (say, 1 day).

查看一段时间内的所有更改(例如1 day)。

$ git whatchanged --since="1 day ago" -p
$ git whatchanged --since="1 day ago" -p <file>   # see changes for a specific file only

回答by OceanWavez

If i understand you correctly, you want to get a diff for a file with one level below HEAD

如果我理解正确,您希望获得比 HEAD 低一级的文件的差异

to check file difference from current HEAD to one level before

检查从当前 HEAD 到前一级的文件差异

git diff HEAD^1 filename

number 1 is for the level you want to compare,

数字 1 是您要比较的级别,

you can get diff using SHA also, to see all commits with their SHA use

您也可以使用 SHA 获取差异,以查看所有使用其 SHA 的提交

git log --oneline

and then you can use the SHA to get a diff to compare current HEAD with specific commit use

然后您可以使用 SHA 获取差异以将当前 HEAD 与特定提交使用进行比较

git diff commitSHA filename

if you want to get all differences between two commit you can use

如果你想获得两次提交之间的所有差异,你可以使用

git diff commitSHA1..commitSHA2 filename

回答by Rafael Kitover

From gitrevisions(7):

来自gitrevisions(7)

The r1^! notation includes commit r1 but excludes all of its parents. By itself, this notation denotes the single commit r1.

r1^!表示法包括提交 r1 但排除其所有父项。就其本身而言,该符号表示单次提交 r1。

This works to show the diff for a single commit, e.g. you can do:

这可以显示单个提交的差异,例如,您可以执行以下操作:

git log --oneline | grep thingamabob

this will give you the short sha, so then you can see the diff for that commit:

这将为您提供简短的 sha,因此您可以看到该提交的差异:

git diff 'b7f57543^!'