User login

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

Searched words: 
git diff of last commit df former commit get git difference between most recent and second most recent HEAD commits

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 as git 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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.