简体   繁体   English

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

[英]Generate diff file of a specific commit in Git

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.当 head 处于特定提交时,我想获取一个 diff 文件,以便我可以将 head 降低到下一级,然后在应用和不应用 diff 文件的情况下尝试测试功能。 So is there a way to generate a diff file of a specific commit.那么有没有办法生成特定提交的差异文件。

Even though there is a way to change the head before and after commit, this method comes in more handy.尽管有一种方法可以在提交之前和之后更改头部,但这种方法更加方便。

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 file changes for a specific commit:查看特定提交的file更改:

git show <commit>:<file>

See all the changes for a time duration (say, 1 day ):查看一段时间内的所有更改(例如1 天):

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

If I understand you correctly, you want to get a diff for a file with one level below HEAD.如果我理解正确,您希望获得比 HEAD 低一级的文件的差异。

To check the file difference from the current HEAD to one level before:要检查从当前 HEAD 到前一级的文件差异:

git diff HEAD^1 filename

The number 1 is for the level you want to compare.数字1是您要比较的级别。

You can also get a diff using the SHA-1 hash also.您还可以使用 SHA-1 哈希获得差异。 To see all commits with their SHA-1 use:要使用 SHA-1 查看所有提交,请使用:

git log --oneline

And then you can use the SHA-1 hash to get a diff to compare the current HEAD with a specific commit.然后您可以使用 SHA-1 哈希来获取差异,以将当前 HEAD 与特定提交进行比较。 Use:用:

git diff commitSHA filename

If you want to get all differences between two commits, you can use:如果要获取两次提交之间的所有差异,可以使用:

git diff commitSHA1..commitSHA2 filename

From gitrevisions(7) :gitrevisions(7)

The r1^! r1^! notation includes commit r1 but excludes all of its parents.表示法包括提交 r1 但排除其所有父项。 By itself, this notation denotes the single commit r1.就其本身而言,该符号表示单次提交 r1。

This works to show the diff for a single commit.这可以显示单个提交的差异。 Eg, you can do:例如,你可以这样做:

git log --oneline | grep thingamabob

This will give you the short SHA-1 hash, so then you can see the diff for that commit:这将为您提供简短的 SHA-1 哈希,因此您可以看到该提交的差异:

git diff 'b7f57543^!'

To generate a diff file you would forward the output to a file.要生成差异文件,您可以将 output 转发到一个文件。 Say commit1 is the commit you want the diff for:commit1是你想要 diff 的提交:

git diff <commit-before-commit1> <commit1> > commit1.diff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM