0

I was trying to get the diff for a commit with the message Add structure shown below, but git diff fb237ff shows the diff for the commit "Add title" instead. How can I get the diff for the SHA I want instead of the child of this commit?:

commit 31013a1 (HEAD -> master, origin/master)
Author: user.name <user.email>
Date:   Sun Jun 17 19:28:52 2018 +0100

    Add title

commit fb237ff
Author: user.name <user.email>
Date:   Sun Jun 17 19:24:33 2018 +0100

    Add structure

commit 69d64b4
Author: user.name <user.email>
Date:   Sun Jun 17 19:10:26 2018 +0100

    Add heading
bit
  • 443
  • 1
  • 7
  • 19
  • Possible duplicate of [How to see the changes in a git commit?](https://stackoverflow.com/questions/17563726/how-to-see-the-changes-in-a-git-commit) – phd Jun 19 '18 at 16:37

3 Answers3

3

git diff SHA shows the differences from the named commit to the current HEAD - which will be everything that has changed since that commit was made - i.e the latest commit.

You probably want git show SHA to show you the changes contained in that commit, or git log -p to show the commit message and changes.

match
  • 10,388
  • 3
  • 23
  • 41
1

git diff accepts one or two revisions to compare. When only one is provided then it compares the working tree against it.

If you posted the output of git log -n 3 then HEAD is 31013a1 and the following Git commands are equivalent:

git diff fb237ff
git diff HEAD~1

If you want to show the changes introduced by the commit fb237ff then you have to compare it with its parent (69d64b4). You can use any of the following commands for this purpose:

git diff 69d64b4 fb237ff
git diff fb237ff~1 fb237ff
git diff HEAD~2 HEAD~1

Read the documentation of git diff and how to specify Git revisions.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Actually, as in the other two answers, `git diff ` compares `` to the work-tree. (I have to consult the `git diff` documentation all the time because its rules for what gets compared to what are kind of counterintuitive in several cases...) – torek Jun 19 '18 at 15:50
1

git diff <commit> shows you the difference between your working directory and <commit>. So in your case, that would indeed be the contents of 31013a1 (plus whatever other uncommited changes you may have).

In order to see the changes that one commit introduced, you can do:

git show <commit>
Christos Batzilis
  • 352
  • 1
  • 3
  • 12