Search code examples
gitgit-diff

What are the differences between these git diff commands?


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

Solution

    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^    <--------------------+