tags:

views:

50

answers:

4

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
A: 

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.

Josh K
You got `--cached` slightly wrong: it shows you only changes that you *have* added to the index (but haven't committed).
Amber
+2  A: 
  1. Diff between HEAD and the working directory.
  2. Diff between the direct ancestor of HEAD and the working directory.
  3. Diff between HEAD and the index.
  4. Diff between the index and the working directory.
wRAR
+8  A: 
  1. Show what has changed since the last commit.
  2. Show what has changed since the commit before the latest commit.
  3. Show what has been added to the index via git add but not yet committed.
  4. Show what has changed but hasn't been added to the index yet via git add.

It looks like this:

Working Directory    <----+--------+-------+
        |                 |        |       |
        |              diff HEAD   |       |
        V                 |        |       |
   "git add"              |        |       |
        |                 |        |     diff
        |                 |        |       |
        V                 |        |       |
     Index     <----+-----|--------|-------+
        |           |     |        |       
        |   diff --staged |        |       
        V           |     |        |       
  "git commit"      |     |        |
        |           |     |        |
        |           |     |        |
        V           |     |        |
      HEAD     <----+-----+        |
        |                          |
        |                       diff HEAD^
        V                          |
previous "git commit"              |
        |                          |
        |                          |
        V                          |
      HEAD^          <-------------+
Amber
@Picasso Is it hand drawn or did you used some ASCII art tool? Is there any git command for this too? ;)
takeshin
@takeshin: Hand-drawn; it's not that hard. (But there is an emacs mode for drawing ascii art, not that I use it.)
Amber
+1  A: 

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".

Matthew Rankin