user1888955
user1888955

Reputation: 626

"Wrong" output by git diff HEAD and git diff

I ran the following commands and got confused about the output by git diff HEAH as well as git diff

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo
$ git init

Initialized empty Git repository in C:/Users/Athrun/Desktop/path/to/my/workspace                                          /demo/.git/

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ echo "Hello World" > welcome.txt

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ git add welcome.txt

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ git commit -m "\"Hello World\" committed"

[master (root-commit) eb00b00] "Hello World" committed
 1 file changed, 1 insertion(+)
 create mode 100644 welcome.txt

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ git rm --cached welcome.txt

rm 'welcome.txt'

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ git diff HEAD

diff --git a/welcome.txt b/welcome.txt
deleted file mode 100644
index 557db03..0000000
--- a/welcome.txt
+++ /dev/null
@@ -1 +0,0 @@
-Hello World

Athrun@Athrun-PC MINGW64 ~/Desktop/path/to/my/workspace/demo (master)
$ git diff

Basically, I initialized a repo, created a file with string "Hello World" and committed the file. Then I ran the command git rm --cached welcome.txt to delete the "file"(not sure whether I can call it "file" here) in staging area. Then I ran two commands git diff HEAD and git diff to compare changes to HEAD and staging area.

What I understand here is that working directory and HEAD have the same version of the file, `git diff HEAD' should show nothing while 'git diff' should show "Hello World" removed.

However, the output is just opposite.

Upvotes: 0

Views: 349

Answers (2)

user4003407
user4003407

Reputation: 22102

When working directory participate in comparison, only tracked files are considered.

  1. git diff HEAD: HEAD have one file welcome.txt, working directory is empty (no files tracked), so diff is welcome.txt removed.
  2. git diff: index is empty, working directory is empty (no files tracked), so diff is empty.

Upvotes: 1

Sudhansu
Sudhansu

Reputation: 200

To my understanding of GIT, below is the explanation for your query.

You have committed the file welcome.txt with "Hello world". So after commit ($ git commit -m "\"Hello World\" committed"), you will get a commit id (say C1). Now your HEAD will point to C1.

After that you changes the file by (git rm --cached welcome.txt) so your working tree changed. Since changes are not committed, your changes are there in your working tree.

1 - git diff HEAD - It is supposed to show difference between HEAD (C1) and working tree, which is "Hello World".

So commit the changes post "git rm" and you should not have any diff.

2 - git diff - Show the difference between staging area and working tree.

But i think this should not be empty. So this is not clear to even me.

Correct me if i am wrong or did not understand your question properly.

Upvotes: 0

Related Questions