Generate diff file of a specific commit in git

狂风中的少年 提交于 2020-05-10 04:07:10

问题


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.

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


回答1:


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.

$ git show <commit>:<file>

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

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



回答2:


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

to check file difference from current HEAD to one level before

git diff HEAD^1 filename

number 1 is for the level you want to compare,

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

git log --oneline

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

git diff commitSHA filename

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

git diff commitSHA1..commitSHA2 filename



回答3:


From gitrevisions(7):

The r1^! notation includes commit r1 but excludes all of its parents. By itself, this notation denotes the single commit 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:

git diff 'b7f57543^!'


来源:https://stackoverflow.com/questions/42357521/generate-diff-file-of-a-specific-commit-in-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!