Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate diff file of a specific commit in Git

Tags:

git

git-diff

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.

like image 675
Ginu Jacob Avatar asked Feb 21 '17 02:02

Ginu Jacob


People also ask

Can you git diff a specific file?

The git diff command returns a list of all the changes in all the files between our last commit and our current repository. If you want to retrieve the changes made to a specific file in a repository, you can specify that file as a third parameter.

How do I get a list of files changed in a specific commit?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

Can you git diff two files?

The git diff command is used to perform the diff function on Git data sources. For example, commits, branches, files, and so on. It can also be used to compare two files of different branches.


4 Answers

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
like image 128
Sajib Khan Avatar answered Oct 27 '22 14:10

Sajib Khan


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
like image 35
OceanWavez Avatar answered Oct 27 '22 14:10

OceanWavez


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^!'
like image 7
Rafael Kitover Avatar answered Oct 27 '22 13:10

Rafael Kitover


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
like image 1
J. Fang Avatar answered Oct 27 '22 12:10

J. Fang