简体   繁体   中英

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. 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:

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

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

To check the file difference from the current HEAD to one level before:

git diff HEAD^1 filename

The number 1 is for the level you want to compare.

You can also get a diff using the SHA-1 hash also. To see all commits with their SHA-1 use:

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. 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) :

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. 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:

git diff 'b7f57543^!'

To generate a diff file you would forward the output to a file. Say commit1 is the commit you want the diff for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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