[git] git diff file against its last change

Is it possible to get git to produce a diff between a specific file as it exists now, and as it existed before the last commit that changed it?

That is, if we know:

$ git log --oneline myfile
123abc Fix some stuff
456def Frobble the foos
789dba Initial commit

Then git diff 456def myfile shows the last change to myfile. Is is possible to do the same without the knowledge produced by the git log; what changed in 123abc?

This question is related to git git-diff

The answer is


If you are fine using a graphical tool this works very well:

gitk <file>

gitk now shows all commits where the file has been updated. Marking a commit will show you the diff against the previous commit in the list. This also works for directories, but then you also get to select the file to diff for the selected commit. Super useful!


One of the ways to use git diff is:

git diff <commit> <path>

And a common way to refer one commit of the last commit is as a relative path to the actual HEAD. You can reference previous commits as HEAD^ (in your example this will be 123abc) or HEAD^^ (456def in your example), etc ...

So the answer to your question is:

git diff HEAD^^ myfile