user53670
user53670

Reputation:

question about git diff

I 'm confused by the output of git diff

echo "hello">>welcome.txt
git diff

Why there is no output?

git add welcome.txt
git diff

Why there is no output?

git diff --cached
diff --git a/welcome.txt b/welcome.txt
new file mode 100644
index 0000000..ce01362
--- /dev/null
+++ b/welcome.txt
@@ -0,0 +1 @@
+hello

Why it output now?

It seems that git diff don't work without a parameter --cached, is it?

Upvotes: 1

Views: 284

Answers (3)

asm
asm

Reputation: 8898

Scenario 1:

echo "hello">>welcome.txt
git diff

The reason for the confusion is mostly that you're creating a new file that's not already in git. Here the file welcome.txt is not in the git repository so git doesn't know anything about it's history. If there had been a previous version of welcome.txt that was committed you would see differences in this case.

Scenario 2:

git add welcome.txt
git diff

In this case you've added the file to the staging area. As far as git diff is concerned this effectively commits the file and git diff will only show you differences between the working copy and the version of welcome.txt in the staging area.

Scenario 3:

As you saw it's not quite true that staging a file is the same as committing it as far as git diff is concerned. You can still pass --cached and you will see the differences between the staging area and the current HEAD.

As Simon said it's mostly about the how git diff interacts with the staging area. Pro Git is Also an excellent online resource for getting started with git.

Edit: Git book entry on treeishes.

Upvotes: 1

Simon
Simon

Reputation: 32953

For this purpose, git add has two meanings:

  1. If the file is not tracked yet, start tracking it and put it in the staging area
  2. If the file is already tracked, put it in the staging area

So when you git diff just after creating the file, you don't see anything. Then when you add it, since it is also directly stagged, you don't see it with git diff but you see it with git diff --cached.

Upvotes: 1

lucapette
lucapette

Reputation: 20724

No, it's all about the staging area. Read more here http://gitready.com/beginner/2009/01/18/the-staging-area.html

Upvotes: 0

Related Questions