Black
Black

Reputation: 20232

Why is git diff only working with option --cached?

If I execute git diff file.php then I don't get any response.

I always have to execute git diff --cached file.php.

Why is --cached not automatically executed and why do I have to do this?

I am using git version 2.17.0.windows.1

Upvotes: 1

Views: 746

Answers (2)

Nghia Bui
Nghia Bui

Reputation: 3784

git diff <file> compares the file in working dir vs in index.

So if you already added the file (by git add) from working dir to index, then:

  1. there is no diff between working dir and index.
  2. but there is diff between the current commit (pointed by HEAD) and index, this diff is shown with the --cached option.

Upvotes: 6

mistiru
mistiru

Reputation: 3366

The purpose of the git diff command is to compare the state of your index with your files present on your filesystem.
That of the git diff --cached command is to compare the state of your index with the commit your are pointing onto.

If you did changes to your file file.php that you did not already stage (using git add), then you should use git diff in order to see the changes.
But if you already did git add on this file, then a copy has been placed in your index, so to see the changes, you have to use the git diff --cached command.

Upvotes: 4

Related Questions