Categories
diff git git-diff version-control

How can I see the changes in a Git commit?

2062

When I do git diff COMMIT I see the changes between that commit and HEAD (as far as I know), but I would like to see the changes that were made by that single commit.

I haven’t found any obvious options on diff / log that will give me that output.

1

2569

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit:

git diff COMMIT~ COMMIT will show you the difference between that COMMIT‘s ancestor and the COMMIT. See the man pages for git diff for details about the command and gitrevisions about the ~ notation and its friends.

Alternatively, git show COMMIT will do something very similar. (The commit’s data, including its diff – but not for merge commits.) See the git show manpage.

(also git diff COMMIT will show you the difference between that COMMIT and the head.)

17

  • 19

    Note that the ^ needs to be quoted in the Thomson and Bourne shells (synonym for | there) and rc and its derivatives (caret operator) and in zsh with extendedglob enabled (not globbing operator)

    Mar 24, 2014 at 14:34


  • 4

    Note that HEAD^ implies first parent in case a commit has multiple parents (ie merge commit).

    – Mansour

    Nov 15, 2015 at 21:19


  • 27

    git diff COMMIT~ COMMIT works for me, notice the tilde instead of caret. I’m running git version 2.6.1.windows.1 on Windows 10.

    Nov 24, 2015 at 10:58


  • 17

    @tradetree: the word COMMIT is supposed to be replaced with the name of some commit, e.g. the SHA sum.

    May 12, 2016 at 19:00

  • 129

    I feel like git show is more appropriate for this question and should be the suggestion mentioned first.

    Oct 12, 2016 at 12:00

620

As mentioned in “Shorthand for diff of git commit with its parent?“, you can also use git diff with:

git diff COMMIT^!

or

git diff-tree -p COMMIT

With git show, you would need (in order to focus on diff alone) to do:

git show --color --pretty=format:%b COMMIT

The COMMIT parameter is a commit-ish:

A commit object or an object that can be recursively dereferenced to a commit object. The following are all commit-ishes: a commit object, a tag object that points to a commit object, a tag object that points to a tag object that points to a commit object, etc.

See gitrevision “SPECIFYING REVISIONS” to reference a commit-ish.
See also “What does tree-ish mean in Git?“.

0

    512

    You can also try this easy way:

    git show <COMMIT>
    

    6

    • 5

      It seems this does something quite different

      Jun 20, 2018 at 18:01

    • 11

      It only shows the commit message. Not the diff of the code changes applied for this commit.

      Sep 10, 2019 at 17:06

    • 21

      This should be the answer.

      – Roel

      Apr 16, 2020 at 16:45

    • 6

      @k0pernikus it does for me

      Sep 9, 2020 at 23:40

    • Sometimes, this command shows the commit message.

      – alfredo

      Mar 14 at 0:48