1

Please, observe (I am using posh-git):

C:\xyz\git [master ↑2 +27 ~0 -0 !]> git diff --name-only
C:\xyz\git [master ↑2 +27 ~0 -0 !]>

27 folders are unstaged, each has some files. No changes are staged.

Why does not it show any files?

EDIT 1

The following simple transcript demonstrates the problem:

C:\xyz> mkdir git


    Directory: C:\xyz


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       10/16/2018   1:20 PM                git


C:\xyz> cd git
C:\xyz\git> git init
Initialized empty Git repository in C:/xyz/git/.git/
C:\xyz\git [master]> echo hello > 1.txt
C:\xyz\git [master +1 ~0 -0 !]> git diff --name-only
C:\xyz\git [master +1 ~0 -0 !]> git status
On branch master

No commits yet

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

        1.txt

nothing added to commit but untracked files present (use "git add" to track)
C:\xyz\git [master +1 ~0 -0 !]>

EDIT 2

According to https://git-scm.com/docs/git-diff :

git diff [<options>] [--] [<path>…​] 

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t. You can stage these changes by using git-add[1].

As far as I understand it means the git diff --name-only command in the transcript above should list 1.txt

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

2

Note that git diff can do a bunch of different things, including (but not limited to):

  • compare one commit to another commit
  • compare any commit to the index
  • compare any commit to the work-tree
  • compare the index to the work-tree

As you just noted in your edit, you are selecting this last option.

When you select to compare the index to the work-tree, the list of files to be compared is completely controlled by the index contents. Any files that are untracked are ignored entirely. This differs from how git status compares the index to the work-tree, but saves a lot of time for git diff since it does not have to find any files that may be untracked (which in general is a slow operation).

If you want Git to know that the files exist, using an index entry that is a sort of placeholder, consider using git add --indent-to-add, or for short, git add -N (uppercase N). I should also mention that this state ("fake-added") was broken for a long time, so there are a number of versions of Git where it behaves weirdly. It was also just updated in 2.19 to correct the git diff output:

  • "git diff" compares the index and the working tree. For paths added with intent-to-add bit, the command shows the full contents of them as added, but the paths themselves were not marked as new files. They are now shown as new by default.
torek
  • 448,244
  • 59
  • 642
  • 775