You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.
To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..
The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file. By default, the git diff command displays any uncommitted changes to your repository.
By default git diff will show you any uncommitted changes since the last commit.
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
.
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.
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 <[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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With