How can I see the changes in a Git commit?
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.
Comments
-
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.-
Chris Maes about 9 yearspossible duplicate of Shorthand for diff of git commit with its parent?
-
-
Stephane Chazelas about 10 yearsNote that the
^
needs to be quoted in the Thomson and Bourne shells (synonym for|
there) andrc
and its derivatives (caret operator) and inzsh
with extendedglob enabled (not
globbing operator) -
Richard almost 10 yearsYour rewording would apply to
git diff HEAD HEAD^
. -
VonC over 9 yearsWould that work too with
git log
? (because of stackoverflow.com/a/18585297/6309) -
user3690202 almost 9 yearsThis doesnt work anymore. git diff HEAD^ HEAD doesn't show anything.
-
user3690202 almost 9 yearsgit diff HEAD^ HEAD doesn't display any changes.
-
Admin almost 9 years@user3690202 so that implies that there aren't any changes to display. Is that actually the case?
-
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^
andHEAD
). This can especially be the case when you do things like ignoring whitespace, or if you simply have to identical commits. -
user3690202 almost 9 yearsHow 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 almost 9 yearsThere 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 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 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 over 8 years@user3690202 use "":
git diff "HEAD^" HEAD
, worked for me -
Mansour over 8 yearsNote that
HEAD^
implies first parent in case a commit has multiple parents (ie merge commit). -
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 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 almost 8 years@tradetree: the word COMMIT is supposed to be replaced with the name of some commit, e.g. the SHA sum.
-
pypmannetjies over 7 yearsI feel like git show is more appropriate for this question and should be the suggestion mentioned first.
-
Lauri Nurmi over 7 yearsIf using bash,
COMMIT^ COMMIT
can be abbreviated asCOMMIT{^,}
. -
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 about 7 yearsYou 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 about 7 yearsThis answer contains too much obscuring information (long introduction about
git diff
which already known by author) -
Martín Coll over 6 yearsWhat does the
^!
mean?? -
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 about 6 yearsI'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 almost 6 yearsIt seems this does something quite different
-
Shibir Basak over 5 yearsThis 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 meangit diff a2a2894 3a1ba8f
or elseunknown option: --stat
. -
brainplot over 5 yearsNote 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 commitHEAD
to commitHEAD^1
? -
k0pernikus over 4 yearsIt only shows the commit message. Not the diff of the code changes applied for this commit.
-
ShellFish over 4 yearsI 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 about 4 yearsThis should be the answer.
-
JobHunter69 over 3 years@k0pernikus it does for me
-
MichaelMoser over 3 yearsgit 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 about 3 years
git diff <commit_Id> on/specific/file.txt
-
CallMarl about 3 years
gitk
is notgit
it's spécifique package and it doesn't exist on every OS wheregit
could be installed.tig
is also good tools as many others. -
xeruf about 3 yearsTo show the changes of the last commit, simply use
git show
;) -
Mecki about 3 years
git show COMMIT
already shows the changeset for normal commits. It only won't show it for merges. -
Peter Mortensen about 3 yearsIs a SHA-1 hash the only possibility (not a rhetorical question)? What about, e.g.,
HEAD~3
? -
Iwnnay about 3 yearsYes, you could use something like
git show HEAD~3 HEAD
It's a little clunkier than other git commands, but it works. -
Jari Turkia almost 3 yearsThere should be command
git diff COMMIT^
(literal). Nobody cares what the most recent commit hash is. -
Weihang Jian over 2 yearsNote that there is no way to show only diff by
git show
. If you want to output only diff, usegit diff
. -
Andrew Falanga over 2 yearsUpvoted 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 about 2 yearsI'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 about 2 yearsSometimes, this command shows the commit message.
-
alfredo about 2 yearsNot works. Only show the commit message
-
Binita Bharati about 2 yearsGreat answer. Like the output format of this command.