Search code examples
gitgit-diff

git diff - compares index to HEAD? What about staging area and HEAD?


So I have been confused about this for a while.

Does git diff, compare the index to HEAD by default? Or the staging area with HEAD?

Assuming git diff compares the index with HEAD, how would I run git diff between staging and HEAD instead of the index and HEAD?


Solution

  • To compare working directory with staging / index use:

    git diff [--options] [--] [<path>…​]
    

    This answers the question What I have changed in files in this repo?

    To compare staging / index with HEAD (<commit> defaults to HEAD) use:

    git diff [--options] --cached [<commit>] [--] [<path>…​]
    

    --cached can be switched with --staged if it easier for you to remember. In documentation index, staging and cached are all used to mean the same thing.

    This answers the question What my next commit will look like?

    Extra
    To compare working directory with HEAD use:

    git diff $(git rev-parse --short HEAD) [--] [<path>…​]