Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between these git diff commands?

Tags:

git

git-diff

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
like image 756
Matthew Rankin Avatar asked Sep 10 '10 16:09

Matthew Rankin


People also ask

What is diff command in git?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

What is the difference between git diff and git diff -- staged?

git diff --staged will only show changes to files in the "staged" area. git diff HEAD will show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.

How does git determine differences?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .

What is A and B in git diff?

In most cases, Git picks A and B in such a way that you can think of A/- as "old" content and B/+ as "new" content. Let's look at our example: Change #1 contains two lines prepended with a "+". Since no counterpart in A existed for these lines (no lines with "-"), this means that these lines were added.


2 Answers

  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^    <--------------------+ 
like image 68
Amber Avatar answered Oct 13 '22 11:10

Amber


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

like image 40
Matthew Rankin Avatar answered Oct 13 '22 12:10

Matthew Rankin