Get a Git diff of the previous commit
Getting a diff (patch-file style display of changes) from a your most recent commit is useful when you commit and it lists three lines changing in a file instead of one. Of course, you should have run git diff --cached
before git commit -m "I know what I'm doing."
, but when you haven't, or have another reason to take one step back in the history of your work, here is the command that saves looking up the git commit hash.
git diff HEAD^ HEAD
(HEAD^ means the previous version of head; leaving off the second HEAD would show any uncommitted changes lying around as well.)
To limit to a particular file, in this case "midbucket.view.inc" in the directory from which we run the command:
git diff HEAD^ HEAD -- midbucket.view.inc
Reference
http://www.kernel.org/pub/software/scm/git/docs/git-diff.html
Comments
Better way
There is a better way:
git show HEAD
This solution is correct, but
This solution is correct, but HEAD is the default parameter for this command, so getting the diff for the last commit can be done as simply as:
git show
Using `git show` is not the same as `git diff HEAD^ HEAD`
Using
git show
is not the same asgit diff HEAD^ HEAD
, at least not for me using Debian 8 Jessie, with git v2.1.4.git diff HEAD^ HEAD
shows all the differences of the previous change, which is what I want to see.Post new comment