2233

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
laktak
  • 57,064
  • 17
  • 134
  • 164
  • 8
    possible duplicate of [Shorthand for diff of git commit with its parent?](http://stackoverflow.com/questions/436362/shorthand-for-diff-of-git-commit-with-its-parent) – Chris Maes Mar 10 '15 at 15:54

26 Answers26

2765

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

yaya
  • 7,675
  • 1
  • 39
  • 38
Nevik Rehnel
  • 49,633
  • 6
  • 60
  • 50
  • 20
    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) – Stephane Chazelas Mar 24 '14 at 14:34
  • 1
    This doesnt work anymore. git diff HEAD^ HEAD doesn't show anything. – user3690202 Jun 07 '15 at 17:16
  • 1
    @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. – Nevik Rehnel Jun 09 '15 at 11:32
  • 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. – user3690202 Jun 09 '15 at 19:07
  • @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 ;) – Nevik Rehnel Jun 10 '15 at 06:35
  • 4
    Note that `HEAD^` implies first parent in case a commit has multiple parents (ie merge commit). – Mansour Nov 15 '15 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. – Juuso Ohtonen Nov 24 '15 at 10:58
  • "git show COMMIT" didn't work for me, but "git show $COMMIT" did. I think this is a typo in the answer? – tradetree May 01 '16 at 20:11
  • 17
    @tradetree: the word COMMIT is supposed to be replaced with the name of some commit, e.g. the SHA sum. – Nick Matteo May 12 '16 at 19:00
  • 142
    I feel like git show is more appropriate for this question and should be the suggestion mentioned first. – pypmannetjies Oct 12 '16 at 12:00
  • 3
    If using bash, `COMMIT^ COMMIT` can be abbreviated as `COMMIT{^,}`. – Lauri Nurmi Jan 12 '17 at 10:34
  • @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. – Tim Goodman Jan 19 '17 at 19:31
  • 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 – Franziska Mar 07 '17 at 10:14
  • 1
    This answer contains too much obscuring information (long introduction about `git diff` which already known by author) – Daniel Garmoshka May 01 '17 at 10:29
  • There should be command `git diff COMMIT^` (literal). Nobody cares what the most recent commit hash is. – Jari Turkia Jun 02 '21 at 09:32
  • Note that there is no way to show only diff by `git show`. If you want to output only diff, use `git diff`. – Weihang Jian Nov 10 '21 at 02:14
  • 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 '^': 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? – transposeglobal Feb 09 '22 at 00:10
  • *NB:* `git show` does not work for stash commits--frustratingly, it only shows the name of the commit but not the file changes. `git diff COMMIT~ COMMIT` works for stash commits. – Jonathan Benn Dec 01 '22 at 20:14
665

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

Pavel
  • 5,374
  • 4
  • 30
  • 55
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
602

You can also try this easy way:

git show <COMMIT>
Reggie Pinkham
  • 11,985
  • 4
  • 38
  • 36
Lakhan
  • 12,328
  • 3
  • 19
  • 28
  • 5
    It seems this does something quite different – Miserable Variable Jun 20 '18 at 18:01
  • 14
    It only shows the commit message. Not the diff of the code changes applied for this commit. – k0pernikus Sep 10 '19 at 17:06
  • 1
    Sometimes, this command shows the commit message. – alfredo Mar 14 '22 at 00:48
  • 4
    For me with git version 2.32.0, I see both the commit message as well as the file diffs/changes from the previous commit (just like the accepted answer does). To be clear, `git show c411d33e` shows both commit message and file changes and `git diff c411d33e~ c411d33e` shows just the file changes. In both cases changes file changes shown are from the commits ancestor. – PatS Jul 14 '22 at 20:52
  • yeah it will only show the log meta-data if it was a rebase or merge i think. use the commit hash in the message if it's there for useful analysis – nate Oct 20 '22 at 19:13
  • 4
    @k0pernikus If you're **not getting the diff** it may be a merge commit. You need `git show --diff-merges=on` in this case. – c z Nov 25 '22 at 10:21
119

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Salma
  • 1,746
  • 1
  • 11
  • 22
71

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.

Zioalex
  • 3,441
  • 2
  • 33
  • 30
64

Use:

git show <commit_sha>

This will show you just what's in that commit. You can do a range by just putting a space between the two commit SHA-1 hashes.

git show <beginning_sha> <ending_sha>

which is pretty helpful if you're rebasing often because your feature logs will all be in a row.

If you happen to want to look at the last 3 commits you can use the HEAD syntax

git show HEAD~3 HEAD
Iwnnay
  • 1,933
  • 17
  • 18
46

For me this works just fine

git show COMMIT --compact-summary

Which shows the next information

Output a condensed summary of extended header information such as file creations or deletions ("new" or "gone", optionally "+l" if it’s a symlink) and mode changes ("+x" or "-x" for adding or removing executable bit respectively) in diffstat. The information is put between the filename part and the graph part. Implies --stat.

Pepe Alvarez
  • 1,218
  • 1
  • 9
  • 15
  • 2
    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_. – Andrew Falanga Dec 29 '21 at 21:04
  • 2
    Great answer. Like the output format of this command. – Binita Bharati Apr 29 '22 at 07:58
  • Here it says: fatal: unrecognized argument: --compact-summary – AG_HIHI Jul 07 '22 at 09:28
  • 1
    @AG_HIHI It sounds to me that your git version does not have that option, you can check here: https://git-scm.com/docs/git-show Search the documentation for your specific git version if the option argument --compact-summary is available, if not, then upgrade to a newer one. – Pepe Alvarez Jul 07 '22 at 17:01
42

First get the commit ID using,

git log #to list all

Or

git log -p -1 #last one commit id

Copy commit id.

Now we use two methods to list changes from a specific commit,

Method 1:

git diff commit_id^! #commit id something like this 1c6a6000asad012

Method 2:

git show commit_id
For example: git show 1c6a600a
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • 3
    What does the `^!` mean?? – Martín Coll Dec 20 '17 at 18:31
  • 3
    ^! is the shorthand for commit^..commit which means will exclude all parents and check diff in that commit – Mohideen bin Mohammed Dec 21 '17 at 07:27
  • 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 – user829755 Apr 03 '18 at 09:16
31

From the man page for git-diff(1):

git diff [options] [<commit>] [--] [<path>…]
git diff [options] --cached [<commit>] [--] [<path>…]
git diff [options] <commit> <commit> [--] [<path>…]
git diff [options] <blob> <blob>
git diff [options] [--no-index] [--] <path> <path>

Use the 3rd one in the middle:

git diff [options] <parent-commit> <commit>

Also from the same man page, at the bottom, in the Examples section:

$ git diff HEAD^ HEAD      <3>

Compare the version before the last commit and the last commit.

Admittedly it's worded a little confusingly, it would be less confusing as

Compare the most recent commit with the commit before it.

  • 3
    Your rewording would apply to `git diff HEAD HEAD^`. – Richard Jun 26 '14 at 15:56
  • git diff HEAD^ HEAD doesn't display any changes. – user3690202 Jun 07 '15 at 17:16
  • @user3690202 so that implies that there aren't any changes to display. Is that actually the case? –  Jun 08 '15 at 02:39
  • 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 Jun 09 '15 at 19:06
  • @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? –  Jun 10 '15 at 00:21
  • @user3690202 use "": `git diff "HEAD^" HEAD`, worked for me – MartenBE Nov 06 '15 at 14:41
27

The following seems to do the job; I use it to show what has been brought in by a merge.

git whatchanged -m -n 1 -p <SHA-1 hash of merge commit>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MichaelMoser
  • 3,172
  • 1
  • 26
  • 26
  • Would that work too with `git log`? (because of http://stackoverflow.com/a/18585297/6309) – VonC Oct 14 '14 at 17:52
  • 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) – MichaelMoser Jan 29 '21 at 01:31
  • Something I learned today. Thanks! **Official DESCRIPTION** ` Shows commit logs and diff output each commit introduces.` New users are encouraged to use git-log(1) instead. The whatchanged command is essentially the same as git-log(1) but defaults to show the raw format diff output and to skip merges. The command is kept primarily for historical reasons; fingers of many people who learned Git long before git log was invented by reading Linux kernel mailing list are trained to type it.` – Kesavan Muthuvel Aug 22 '23 at 17:11
23

Another possibility:

git log -p COMMIT -1
frogatto
  • 28,539
  • 11
  • 83
  • 129
John_West
  • 2,239
  • 4
  • 24
  • 44
16

I like the below command to compare a specific commit and its last commit:

git diff <commit-hash>^-

Example:

git diff cd1b3f485^-
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
starcwl
  • 396
  • 2
  • 9
12

You could use git diff HEAD HEAD^1 to see the diff with the parent commit.

If you only want to see the list of files, add the --stat option.

Irshu
  • 8,248
  • 8
  • 53
  • 65
  • 1
    This is what you mean, git diff HEAD^1 HEAD – Shibir Basak Sep 26 '18 at 11:34
  • 2
    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`? – brainplot Jan 25 '19 at 00:34
11
git difftool COMMIT^ <commit hash>

is also possible if you have configured your difftool.

See here how to configure difftool. Or the manual page here.

Additionally, you can use git diff-tree --no-commit-id --name-only -r <commit hash> to see which files been changed/committed in a give commit hash.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
10

To see author and time by commit, use git show COMMIT. Which will result in something like this:

commit 13414df70354678b1b9304ebe4b6d204810f867e
Merge: a2a2894 3a1ba8f
Author: You <you@you.com>
Date:   Fri Jul 24 17:46:42 2015 -0700

     Merge remote-tracking branch 'origin/your-feature'

If you want to see which files had been changed, run the following with the values from the Merge line above, git diff --stat a2a2894 3a1ba8f.

If you want to see the actual diff, run git --stat a2a2894 3a1ba8f.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikita R.
  • 7,245
  • 3
  • 51
  • 62
  • "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`. – 林果皞 Nov 21 '18 at 08:56
  • `git show COMMIT` already shows the changeset for normal commits. It only won't show it for merges. – Mecki Mar 26 '21 at 10:51
9

For checking complete changes:

  git diff <commit_Id_1> <commit_Id_2>

For checking only the changed/added/deleted files:

  git diff <commit_Id_1> <commit_Id_2> --name-only

NOTE: For checking diff without commit in between, you don't need to put the commit ids.

bit_cracker007
  • 2,341
  • 1
  • 26
  • 26
8

If you just want to see the changes in the latest commit, simply git show will give you that.

MyrionSC2
  • 1,248
  • 1
  • 14
  • 24
8

The following code will show the current commit

git show HEAD
jinson
  • 331
  • 4
  • 10
6

More minimalist approach for checking file changes (example)

# 1. Checkout a branch and see the list of commits
git log --oneline -5

# > Output
9b9b1f8 (HEAD -> master) Updated ABC
d58e5da chore: Added files
5a4aa2c chore: Added my pipeline
bb2b0b7 feat: Added ABC
473f711 feat: Added ZYX
# 2. Pick a commit hash and check which files were modified
git show --stat --oneline d58e5da

# > Output
d58e5da chore: Added versioning files
 Someabcfile                            | 18 ++++++++++++++++++
 myfolder/file.py                       | 19 +++++++++++++++++++
 myfolder/file.js                       |  7 +++++++
 myfolder/file.txt                      |  1 +
 4 files changed, 45 insertions(+)
# 3. Pick a file to check the differences
git show d58e5da myfolder12/file.py

Or, alternatively, check all file differences within a single commit from the list:

git show d58e5da
Bersan
  • 1,032
  • 1
  • 17
  • 28
5

A few answers miss a special case. How to view changes made by the Root Commit as it does not have a parent/ancestor.

Both

git diff <root_commit>^..<root_commit>

and

git diff <root_commit>~..<root_commit>

throw an error.

$git diff 27e521ca73a46b2d3a28568dc49fced81e46aaea~ 27e521ca73a46b2d3a28568dc49fced81e46aaea
fatal: ambiguous argument '27e521ca73a46b2d3a28568dc49fced81e46aaea~': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

git diff <root_commit>^!

shows diff btw root commit and HEAD. Like so:

$ git diff 27e521ca73a46b2d3a28568dc49fced81e46aaea^!
diff --git a/file1.txt b/file1.txt
new file mode 100644
index 0000000..80f3f1a
--- /dev/null
+++ b/file1.txt
@@ -0,0 +1,5 @@
+Create the first file.
+
+Add some placeholder text to first file.
+
+
diff --git a/file2.txt b/file2.txt
new file mode 100644
index 0000000..66e494f
--- /dev/null
+++ b/file2.txt
@@ -0,0 +1,6 @@
+This is the second file.
+
+It has an uncommited commit.
+
+We use it to demo default `git diff` behaviour.
+

(These are changes made by all commits btw my root commit and HEAD).

For Root Commit

I find only

git show --color --pretty=format:%b <root_commit_hash>

works.

Like so:

$ git show --color --pretty=format:%b 27e521ca73a46b2d3a28568dc49fced81e46aaea

diff --git a/README b/README
new file mode 100644
index 0000000..12a04f0
--- /dev/null
+++ b/README
@@ -0,0 +1,6 @@
+# git-diff-demo
+
+This repo documents the demo of the git diff command.
+We will have options, and use cases.

(My root commit added only the README)

4

This command will get you the Git parent commit-hash:

git log -n 2 <commit-hash>

After that git diff-tool <commit-hash> <parent-commit-hash>

Example:

bonnie@bonnie ~/ $ git log -n 2 7f65b9a9d3820525766fcba285b3c678e889fe3

commit 7f65b9a9d3820525766fcba285b3c678e889fe3b
Author: souparno <souparno.majumder@gmail.com>
Date:   Mon Jul 25 13:17:07 2016 +0530

CSS changed to maintain the aspect ratio of the channel logos and to fit them properly.

commit c3a61f17e14e2b80cf64b172a45f1b4826ee291f
Author: souparno <souparno.majumder@gmail.com>
Date:   Mon Jul 25 11:28:09 2016 +0530

The ratio of the height to width of the channel images are maintained.

After this

git difftool 7f65b9a9d3820525766fcba285b3c678e889fe3b c3a61f17e14e2b80cf64b172a45f1b4826ee291f
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
4

I'm running Git version 2.6.1.windows.1 on Windows 10, so I needed a slight modification to Nevik's answer (tilde instead of caret):

git diff COMMIT~ COMMIT

Another option is to quote the caret:

git diff "COMMIT^" COMMIT
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
4

Get list of files changed in a commit:

git show --name-only commit_id

Note: Above command will not work for merge ids.


To get list of files changed in a merge commit id:

git log -m -1 --name-only commit_id


View changes in a specific file within a commit: git show commit_id:src/path/to/that/file

Bharat Pahalwani
  • 1,404
  • 3
  • 25
  • 40
  • it's better to use `git show commit_id -- src/path/to/that/file` to get color in output. – Eric Aug 31 '23 at 02:22
3

It is also possible to review changes between two commits for a specific file.

git diff <commit_Id_1> <commit_Id_2> some_dir/file.txt
Sergey Miletskiy
  • 477
  • 2
  • 5
  • 13
2

In case of checking the source change in a graphical view, use:

gitk (your commit id goes here)

For example:

gitk HEAD~1 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 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. – ShellFish Sep 25 '19 at 13:12
  • 2
    `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. – CallMarl Feb 12 '21 at 10:32
-4
  1. You can click on each commit on the specific address of git to view
  2. If you submit with a tool, you can pass show history
yiyi
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '22 at 13:33
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32778316) – abdo Salm Sep 28 '22 at 02:12