See diff between current state and last commit

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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 - Git

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 - Git

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

Solution 6 - Git

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:

https://git-scm.com/docs/gitrevisions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontravis1097View Question on Stackoverflow
Solution 1 - GitKlas MellbournView Answer on Stackoverflow
Solution 2 - GittallamjrView Answer on Stackoverflow
Solution 3 - GitakedView Answer on Stackoverflow
Solution 4 - GitCarlos Martín NietoView Answer on Stackoverflow
Solution 5 - GitAbimael DomínguezView Answer on Stackoverflow
Solution 6 - GitMartin FlaskaView Answer on Stackoverflow