What are the differences between these git diff commands?

GitGit Diff

Git Problem Overview


What are the differences between the following git commands?

  1. git diff HEAD
  2. git diff HEAD^
  3. git diff --cached or the synonym git diff --staged
  4. git diff

Git Solutions


Solution 1 - Git

  1. git diff HEAD - Shows what has changed since the last commit.
  2. git diff HEAD^ - Shows what has changed since the commit before the latest commit.
  3. git diff --cached - Show what has been added to the index via git add but not yet committed.
  4. git diff - Show what has changed but hasn't been added to the index yet via git add.

It looks like this:

     Working
    Directory  <----+--------+------+
        |           |        |      |    
        |           |        |      |
        V           |        |      |    
    "git add"       |        |      |    
        |         diff       |      |    
        |           |        |      |    
        V           |        |      |    
     Index     <----+    diff HEAD  |            
        |           |        |      |       
        |           |        |      |
        V           |        |      |       
  "git commit"      |        |      |
        |     diff --cached  |      |
        |     diff --staged  |      |
        V           |        |      |
      HEAD     <----+--------+      |
        |                           |
        |                        diff HEAD^
        V                           |
previous "git commit"               |
        |                           |
        |                           |
        V                           |
      HEAD^    <--------------------+

Solution 2 - Git

  1. git diff HEAD : Diff between HEAD and the working directory.
  2. git diff HEAD^ : Diff between the direct ancestor of HEAD and the working directory.
  3. git diff --cached or the synonym git diff --staged : Diff between HEAD and the index.
  4. git diff : Diff between the index and the working directory.

Solution 3 - Git

From the Git Community Book:

git diff

> will show you changes in the working directory that are not yet staged for the next commit.

git diff --cached

> will show you the difference between the index and your last commit; what you would be committing if you run "git commit" without the "-a" option.

git diff HEAD

> shows changes in the working directory since your last commit; what you would be committing if you run "git commit -a".

Solution 4 - Git

Here's a simple way to remember these commands:

  • By default, git diff's source is assumed to be the working directory, and its target is the index.
  • Adding the --cached flag changes the source to be the index. It doesn't necessarily change the target once you add more arguments. But for convenience the target becomes the last commit if none is provided.
  • Adding a commit as an argument changes the target.
  • Adding two commits as arguments changes both the source and the target, respectively.

Have fun mixing them up to your own liking :)

Solution 5 - Git

HEAD is the current HEAD pointer in the tree, HEAD^ is the commit before HEAD. --cached I'm not sure about.--cached will show you any changes you have made but haven't added to the index.

The git tutorial on kernal.org is a very good read.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew RankinView Question on Stackoverflow
Solution 1 - GitAmberView Answer on Stackoverflow
Solution 2 - GitwRARView Answer on Stackoverflow
Solution 3 - GitMatthew RankinView Answer on Stackoverflow
Solution 4 - GitgunbyView Answer on Stackoverflow
Solution 5 - GitJosh KView Answer on Stackoverflow