Difference between a git commit and the working directory?
Solution 1
Yes, but it depends a bit on your definition on what the “current project state” is. The manual for git diff goes on.
If you are interested in comparing with the staged changes:
git diff [--options] --cached [<commit>] [--] [<path>...]
This form is to view the changes you staged for the next commit relative to the named
<commit>
. Typically you would want comparison with the latest commit, so if you do not give<commit>
, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and<commit>
is not given, it shows all staged changes.--staged
is a synonym of--cached
.
If you are interested in comparing with the unstaged changes:
git diff [--options] <commit> [--] [<path>...]
This form is to view the changes you have in your working tree relative to the named
<commit>
. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.
Or if you are just interested in comparing any two commits (one could be HEAD):
git diff [--options] <commit> <commit> [--] [<path>...]
This is to view the changes between two arbitrary
<commit>
s.
So you might want to run git diff someOldCommit HEAD
to see the differences between someOldCommit
and the current HEAD.
Solution 2
It ist just simple:
git diff HEAD
Explanation: Current changes in the working directory compared with the last commit.
Related videos on Youtube
HelloGoodbye
Updated on June 05, 2020Comments
-
HelloGoodbye almost 4 years
The git-diff manual pages says that git diff is used to
Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.
But what about if you want to show the difference between a commit prior to HEAD and the working directory? Is that possible?
-
HelloGoodbye over 10 yearsBy "the current project state" I was referring to the working directory, I know that was a bit diffuse. I've updated my question.
-
HelloGoodbye over 10 yearsThanks for the answer. So,
git diff [--options] <commit> [--] [<path>...]
was what I was looking for then. In my case, as I don't need to use any options, and I only need to get differences within the current path, for me this reduces togit diff <commit> .
-
HelloGoodbye almost 9 yearsOr, well, since I want to diff the whole repository, and I'm not guaranteed to be in the top-level directory, I would need
git diff <commit> <path-to-top-level-directory>
. From a script this would begit diff <commit> "$(git rev-parse --show-toplevel)"
. -
jlh over 6 yearsActually you can omit the
.
entirely. Just usegit diff <commit>
to compare a commit to the current work directory. -
alpha_989 almost 6 years@poke, @jlh, what does a
commit SHA
mean togit
. Does acommit SHA
represent the entire project directory or does the represent thecommit-diff
between that commit and the one before? Any reference about this? if acommit
represents the state of the project at that time.. then it makes sense thatgit diff commit
would give you the diff between the current working directory and the state of the project for that commit. -
poke almost 6 years@alpha_989 A commit hash is the unique id of a commit. A commit is a snapshot of a version of the repository, including pointers to the previous history. So when you do
git diff some-hash
, you are comparing the version atsome-hash
with the current working directory. -
HelloGoodbye almost 5 yearsMy question was how to show the difference between a commit prior to HEAD and the working directory.
-
Lonely almost 5 yearsthis answer is for me :)
-
Ahmad Ismail over 2 years@Lonely thank you for clarifying because this is a wrong answer. Actually, git diff is between index and working tree. It just so happens that until you have staged changes to the index (with git add) that its contents will be identical to the HEAD commit.
-
Lonely over 2 yearsthis answer is for me