Sometimes when I'm about to make a commit, I can't recall exactly what has changed since the last commit. How can I see a diff of the current state of the code and the last commit?
6 Answers
If you haven't added any files to the index yet (with git add
), simply do
git diff
This will show the diff between your working tree and index.
If you have added files to the index, you need to do this to show the differences between index and the last commit (HEAD).
git diff --cached
Finally, if you want to see the changes made in the working tree compared to the latest commit (HEAD
) you can (as Carlos points out) do
git diff HEAD
Those changes are the combination of git diff
and git diff --cached
.
-
3
git diff
compares the worktree against the index, not HEAD. Jun 21, 2013 at 21:24 -
@CarlosMartínNieto You are correct, sir. I have improved my post to reflect this. Jun 21, 2013 at 22:09
-
I run that, but the terminal, doesn't end, how can I exit.? nothing works. May 2, 2014 at 23:06
-
2
-
1
If you have just made a commit, or want to see what has changed in the last commit compared to the current state (assuming you have a clean working tree) you can use:
git diff HEAD^
This will compare the HEAD with the commit immediately prior. One could also do
git diff HEAD^^
to compare to the state of play 2 commits ago. To see the diff between the current state and a certain commit, just simply do:
git diff b6af6qc
Where b6af6qc
is an example of a commit hash.
-
3A heads-up for Windows cmd version of git - you have to escape caret(^) character with the caret itself. Therefore, "git diff HEAD^" shall be "git diff HEAD^^". In the same way, "HEAD^^" shall be input as "HEAD^^^^".– EellorAug 30, 2018 at 16:05
-
1In this answer I think it is a bit unclear what "last commit" refers to.
HEAD^
is the commit before the latest commit. Aug 28, 2019 at 18:30
this also shows the difference and what files has been changed/modified.
$ git status
Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by git (and are not ignored by gitignore(5)). The first are what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit.
https://www.kernel.org/pub/software/scm/git/docs/git-status.html
You ask git to diff the current/last commit, which has a shorthand of HEAD
.
So git diff HEAD
will compare the current state of the worktree with the current commit.
Have you ever tried git show
?
DESCRIPTION: Shows one or more objects (blobs, trees, tags and commits).
For commits it shows the log message and textual diff. It also presents the merge commit in a special format as produced by git diff-tree --cc.
taken from git help
You don't need to write HEAD or the SHA-1 of the last commit, only type git show
.
I think that it would be helpful for your needs as well as the other answers but with a little less typing and more information depending on the case.
Here I add a sample of what git show
actually shows:
>> git show
commit 49832d33b5329fff95ba0a86002ee8d5a762f3ae (HEAD -> my_new_branch, master)
Author: Abimael Domínguez <my_mail@mail.com>
Date: Thu Jan 7 13:05:38 2021 -0600
This is the commit message of the last commit
diff --git a/some_folder/some_file.txt b/some_folder/some_file.txt
index 59fb665..5c36cde 100644
--- a/some_folder/some_file.txt
+++ b/some_folder/some_file.txt
@@ -3,6 +3,6 @@
This is the content of the last updated file
some text
some text
-text deleted
+text added
some text
some text
This also works for me:
# The last one
git diff HEAD~1 HEAD
# The last but one, etc...
git diff HEAD~2 HEAD~1
This usually works for a linear history. This could get more tricky if there are also merge commits. I recommend you to look into this doc for a nice and complete explanation, especially that commit tree illustration example: