1

Git diff man page says

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk.

I am little confused here for the untracked files in relation to bold part of the definition. Here is what I did

touch test.html
git status

' it shows

blr-mpjfs:workflow_manager amazumde$ git status

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)


test.html

but when I do git diff nothing shows up here If I add the file using git add git diff shows the difference

I took a look at Can I use git diff on untracked files? Still not sure.

So here is my question "Does git diff show difference between working tree and index for untracked/ newly created files in working directory"?

EDIT

If it is not supposed to show the diff for untracked files, isn't the description little unclear? Is there any place where it is clearly called out?

Second Edit

Also how differently then git status internally works than git diff to show untracked files ?

Community
  • 1
  • 1
Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44

3 Answers3

3

An "untracked file" is, by definition, not in the index. (This does not seem to be properly documented, but a "tracked file" is simply one that has an entry in the index, and an "untracked file" is one that does not. This includes the special case of an index entry that records the intent to delete a file: such a file is tracked, even though the index entry for that file says "don't include this file".)

Your question therefore begins by claiming that path P is not in the index (i.e., P is untracked), and then asks whether git can compare the file found in the index via path P to some other file. As you can see, it is difficult to answer this question, since it makes two contradictory claims: that P is not in the index, and simultaneously, that P is in the index.

torek
  • 448,244
  • 59
  • 642
  • 775
  • perfect!! just one follow up question. Then how does git status works? – Abhijit Mazumder Mar 03 '15 at 12:18
  • `git status` essentially runs two comparisons: "HEAD vs index" to tell you what is set up to go into the next commit, and "index vs work-dir" to tell you what is not yet set up to happen. The latter is special-cased so that if there is a file in the work dir that is not in the index, it *may* draw an "untracked" complaint or "??" status in `--short` output (but first `git status` checks for an ignore/exclude entry). So that one particular case (which is not `git diff` itself) is where "untracked"-ness can be announced. – torek Mar 03 '15 at 16:06
1

No, git diff can't show difference between working tree and index for untracked created files in working directory, and neither does SVN.

jansesun
  • 108
  • 5
0

Your question is a possible duplicate of this can-i-use-git-diff-on-untracked-files

As also mentioned in those answers you can do something like git diff --no-index tracked_file untracked_file (Answer from @Harold)

Community
  • 1
  • 1
rogaa
  • 370
  • 2
  • 16