Ginu Jacob
Ginu Jacob

Reputation: 1818

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.

Upvotes: 46

Views: 61232

Answers (4)

Sajib Khan
Sajib Khan

Reputation: 24194

See the changes of a specific commit:

git diff <commit-sha>^!

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

Upvotes: 53

J. Fang
J. Fang

Reputation: 41

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

Upvotes: 4

Rafael Kitover
Rafael Kitover

Reputation: 1087

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-1 hash, so then you can see the diff for that commit:

git diff 'b7f57543^!'

Upvotes: 15

OceanWavez
OceanWavez

Reputation: 303

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

Upvotes: 19

Related Questions