Difference between a git commit and the working directory?

28,286

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.

Share:
28,286

Related videos on Youtube

HelloGoodbye
Author by

HelloGoodbye

Updated on June 05, 2020

Comments

  • HelloGoodbye
    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
    HelloGoodbye over 10 years
    By "the current project state" I was referring to the working directory, I know that was a bit diffuse. I've updated my question.
  • HelloGoodbye
    HelloGoodbye over 10 years
    Thanks 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 to git diff <commit> .
  • HelloGoodbye
    HelloGoodbye almost 9 years
    Or, 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 be git diff <commit> "$(git rev-parse --show-toplevel)".
  • jlh
    jlh over 6 years
    Actually you can omit the . entirely. Just use git diff <commit> to compare a commit to the current work directory.
  • alpha_989
    alpha_989 almost 6 years
    @poke, @jlh, what does a commit SHA mean to git. Does a commit SHA represent the entire project directory or does the represent the commit-diff between that commit and the one before? Any reference about this? if a commit represents the state of the project at that time.. then it makes sense that git diff commit would give you the diff between the current working directory and the state of the project for that commit.
  • poke
    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 at some-hash with the current working directory.
  • HelloGoodbye
    HelloGoodbye almost 5 years
    My question was how to show the difference between a commit prior to HEAD and the working directory.
  • Lonely
    Lonely almost 5 years
    this answer is for me :)
  • Ahmad Ismail
    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
    Lonely over 2 years
    this answer is for me