Generate diff file of a specific commit in git

2020-06-02 10:26发布

问题:

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^!'


标签: git git-diff