What are the differences between these git diff commands?

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

Leave a Comment