Reputation: 60003
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.
Upvotes: 2378
Views: 2017449
Reputation: 1698
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
Upvotes: 16
Reputation: 3463
# 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
Upvotes: 10
Reputation: 1
Upvotes: -5
Reputation: 81
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)
Upvotes: 6
Reputation: 1624
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.
Upvotes: 53
Reputation: 1324278
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?".
Upvotes: 689
Reputation: 51945
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.)
Upvotes: 2897
Reputation: 2008
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
Upvotes: 70
Reputation: 29
In case of checking the source change in a graphical view, use:
gitk (your commit id goes here)
For example:
gitk HEAD~1
Upvotes: 2
Reputation: 1836
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.
Upvotes: 128
Reputation: 7493
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 <[email protected]>
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
.
Upvotes: 10
Reputation: 6784
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.
Upvotes: 11
Reputation: 487
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
Upvotes: 3
Reputation: 1318
If you just want to see the changes in the latest commit, simply git show
will give you that.
Upvotes: 8
Reputation: 4303
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.
Upvotes: 78
Reputation: 2569
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.
Upvotes: 9
Reputation: 8446
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.
Upvotes: 12
Reputation: 406
I like the below command to compare a specific commit and its last commit:
git diff <commit-hash>^-
Example:
git diff cd1b3f485^-
Upvotes: 17
Reputation: 20137
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
Upvotes: 43
Reputation: 9662
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
Upvotes: 4
Reputation: 2052
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 <[email protected]>
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 <[email protected]>
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
Upvotes: 4
Reputation: 3500
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>
Upvotes: 28
Reputation:
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.
Upvotes: 31