为什么git diff –name-only不显示任何修改过的文件?


Why git diff --name-only does not show any modified files?

请注意(我正在使用posh git):

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

27个文件夹未分页,每个文件夹都有一些文件。未进行任何更改。

为什么不显示任何文件?

编辑1

以下简单的文字说明了问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 !]>

编辑2

根据https://git-scm.com/docs/git-diff:

1
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].

据我所知,这意味着上述文本中的git diff --name-only命令应列出1.txt。


注意,git diff可以做很多不同的事情,包括(但不限于):

  • 将一个提交与另一个提交进行比较
  • 比较对索引的任何提交
  • 比较对工作树的任何提交
  • 将索引与工作树进行比较

正如您在编辑中所指出的,您选择的是最后一个选项。

当您选择将索引与工作树进行比较时,要比较的文件列表完全由索引内容控制。任何未跟踪的文件都将被完全忽略。这与git status将索引与工作树进行比较的方式不同,但由于git diff不需要查找任何可能未跟踪的文件(一般来说,这是一个缓慢的操作),因此它为git diff节省了大量时间。

如果您希望Git知道文件存在,使用一种作为占位符的索引项,可以考虑使用git add --indent-to-add,或者简而言之,使用git add -N(大写N)。我还应该提到这个状态("假添加")被破坏了很长一段时间,所以有很多版本的Git行为怪异。它也在2.19中进行了更新,以纠正git diff输出:

  • "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.