How can I see the changes in a Git commit?

1,468,885

Solution 1

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.)

Solution 2

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?".

Solution 3

You can also try this easy way:

git show <COMMIT>

Solution 4

git show shows the changes made in the most recent commit. It is equivalent to git show HEAD.

git show HEAD~1 takes you back one commit.

Solution 5

I usually do:

git diff HEAD~1

To show the changes regarding the last commit. If you have more commits just increase the number 1 to how many commits diff you want to see.

Share:
1,468,885
laktak
Author by

laktak

My backslash escaped!

Updated on April 08, 2022

Comments

  • laktak
    laktak about 2 years

    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.

  • Stephane Chazelas
    Stephane Chazelas about 10 years
    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)
  • Richard
    Richard almost 10 years
    Your rewording would apply to git diff HEAD HEAD^.
  • VonC
    VonC over 9 years
    Would that work too with git log? (because of stackoverflow.com/a/18585297/6309)
  • user3690202
    user3690202 almost 9 years
    This doesnt work anymore. git diff HEAD^ HEAD doesn't show anything.
  • user3690202
    user3690202 almost 9 years
    git diff HEAD^ HEAD doesn't display any changes.
  • Admin
    Admin almost 9 years
    @user3690202 so that implies that there aren't any changes to display. Is that actually the case?
  • Nevik Rehnel
    Nevik Rehnel almost 9 years
    @user3690202, yes it does. If the diff doesn't show anything, it means the diff is empty (there's no difference between HEAD^ and HEAD). This can especially be the case when you do things like ignoring whitespace, or if you simply have to identical commits.
  • user3690202
    user3690202 almost 9 years
    How can there not be any changes to display? If you want to view the last commit, surely unless it is a completely new repository there will be some changes to display?
  • user3690202
    user3690202 almost 9 years
    There was definitely a non-whitespace commit there - I know because I just committed it literally 5 minutes before trying to view what I committed. There must be some other situations where this doesn't work.
  • Admin
    Admin almost 9 years
    @user3690202 it's possible to make an "empty commit" with Git that doesn't actually contain any changes from the parent, although there is a built-in safeguard that checks for and prevents this, though it is overridable with a command line option. I doubt that you would intentionally create an empty commit, so another possibility is that you somehow have pre-commit line-ending conversion on (or other funny whitespace stuff) that is tricking Git into thinking that no changes have actually been made. What platform are you running Git on?
  • Nevik Rehnel
    Nevik Rehnel almost 9 years
    @user3690202, if that continues to be a problem (or just interests you), you should open a new question describing your situation in detail and trying to reproduce it ;)
  • MartenBE
    MartenBE over 8 years
    @user3690202 use "": git diff "HEAD^" HEAD, worked for me
  • Mansour
    Mansour over 8 years
    Note that HEAD^ implies first parent in case a commit has multiple parents (ie merge commit).
  • Juuso Ohtonen
    Juuso Ohtonen over 8 years
    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.
  • tradetree
    tradetree about 8 years
    "git show COMMIT" didn't work for me, but "git show $COMMIT" did. I think this is a typo in the answer?
  • Nick Matteo
    Nick Matteo almost 8 years
    @tradetree: the word COMMIT is supposed to be replaced with the name of some commit, e.g. the SHA sum.
  • pypmannetjies
    pypmannetjies over 7 years
    I feel like git show is more appropriate for this question and should be the suggestion mentioned first.
  • Lauri Nurmi
    Lauri Nurmi over 7 years
    If using bash, COMMIT^ COMMIT can be abbreviated as COMMIT{^,}.
  • Tim Goodman
    Tim Goodman over 7 years
    @JuusoOhtonen In theory a single ^ would do the same thing as a single ~, but I believe you need to put quotes around the ^ as Stephane suggested. That's what I found to be the case when running git from the Windows command prompt.
  • Franziska
    Franziska about 7 years
    You can also extend this command with file specification (for example to exclude fixture files): git diff COMMITID^ COMMITID *.yml <--- shows only changed yml files
  • Daniel Garmoshka
    Daniel Garmoshka about 7 years
    This answer contains too much obscuring information (long introduction about git diff which already known by author)
  • Martín Coll
    Martín Coll over 6 years
    What does the ^! mean??
  • Mohideen bin Mohammed
    Mohideen bin Mohammed over 6 years
    ^! is the shorthand for commit^..commit which means will exclude all parents and check diff in that commit
  • user829755
    user829755 about 6 years
    I'm not an expert but I have a case (with multiple branches being involved) where git log c^! is not exactly the same as git log c^..c. In fact it's much better: git log c^..c listed too many entries while git log c^! did the right thing, so this is what I was looking for for a long time
  • Miserable Variable
    Miserable Variable almost 6 years
    It seems this does something quite different
  • Shibir Basak
    Shibir Basak over 5 years
    This is what you mean, git diff HEAD^1 HEAD
  • 林果皞
    林果皞 over 5 years
    "If you want to see the actual diff, run git --stat a2a2894 3a1ba8f". I think you mean git diff a2a2894 3a1ba8f or else unknown option: --stat.
  • brainplot
    brainplot over 5 years
    Note that this will show what you added as removed, as it will do a reverse comparison. The way you should read the diff command is: what would I need to change in the file to get from commit HEAD to commit HEAD^1?
  • k0pernikus
    k0pernikus over 4 years
    It only shows the commit message. Not the diff of the code changes applied for this commit.
  • ShellFish
    ShellFish over 4 years
    I don't understand why this answer was downvoted. I agree that command line and text based stuff is the way to go but gitk gives a lot of useful information.
  • Roel
    Roel about 4 years
    This should be the answer.
  • JobHunter69
    JobHunter69 over 3 years
    @k0pernikus it does for me
  • MichaelMoser
    MichaelMoser over 3 years
    git log --name-only - for listing the changed files. Or git log --name-status --find-renames - to get the list of changed files with the kind of change (added/modified/renamed, etc)
  • CallMarl
    CallMarl about 3 years
    git diff <commit_Id> on/specific/file.txt
  • CallMarl
    CallMarl about 3 years
    gitk is not git it's spécifique package and it doesn't exist on every OS where git could be installed.tig is also good tools as many others.
  • xeruf
    xeruf about 3 years
    To show the changes of the last commit, simply use git show ;)
  • Mecki
    Mecki about 3 years
    git show COMMIT already shows the changeset for normal commits. It only won't show it for merges.
  • Peter Mortensen
    Peter Mortensen about 3 years
    Is a SHA-1 hash the only possibility (not a rhetorical question)? What about, e.g., HEAD~3?
  • Iwnnay
    Iwnnay about 3 years
    Yes, you could use something like git show HEAD~3 HEAD It's a little clunkier than other git commands, but it works.
  • Jari Turkia
    Jari Turkia almost 3 years
    There should be command git diff COMMIT^ (literal). Nobody cares what the most recent commit hash is.
  • Weihang Jian
    Weihang Jian over 2 years
    Note that there is no way to show only diff by git show. If you want to output only diff, use git diff.
  • Andrew Falanga
    Andrew Falanga over 2 years
    Upvoted because this was actually what I was looking for. I stumbled onto this question because "see changes in commit" can mean a few different things. I wanted to see the files that changed, not actually what changed in them. Keep reading for very good answers to seeing what changed in the file.
  • transposeglobal
    transposeglobal about 2 years
    I'm trying to run these commands on a GitHub action that uses Ubuntu, and it doesn't like git diff $GITHUB_SHA^ $GITHUB_SHA. It gives the error: fatal: ambiguous argument '<GITHUB_SHA>^': unknown revision or path not in the working tree. I've also tried with the ~. Any ideas on how to get this to work in a Github action workflow?
  • alfredo
    alfredo about 2 years
    Sometimes, this command shows the commit message.
  • alfredo
    alfredo about 2 years
    Not works. Only show the commit message
  • Binita Bharati
    Binita Bharati about 2 years
    Great answer. Like the output format of this command.