See diff between current state and last commit
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?
Solution 1:
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
.
Solution 2:
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.
Solution 3:
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
Solution 4:
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.
Solution 5:
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 <[email protected]>
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