Difference between a git commit and the working directory?

HelloGoodbye picture HelloGoodbye · Aug 8, 2013 · Viewed 24.4k times · Source

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?

Answer

poke picture poke · Aug 8, 2013

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.