简体   繁体   English

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

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

Please, observe (I am using posh-git): 请注意(我正在使用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. 27个文件夹是未暂存的,每个文件夹都有一些文件。 No changes are staged. 没有上演任何更改。

Why does not it show any files? 为什么不显示任何文件?

EDIT 1 编辑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 编辑2

According to https://git-scm.com/docs/git-diff : 根据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. 换句话说,差异就是您可以告诉Git进一步添加到索引中的内容,但您仍然没有。 You can stage these changes by using git-add[1]. 您可以使用git-add [1]进行这些更改。

As far as I understand it means the git diff --name-only command in the transcript above should list 1.txt 据我了解,这意味着上面成绩单中的git diff --name-only命令应列出1.txt

Note that git diff can do a bunch of different things, including (but not limited to): 请注意, git diff可以做很多不同的事情,包括(但不限于):

  • 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). 这与git status将索引与工作树比较的方式不同,但是由于git diff不必查找任何可能无法跟踪的文件(通常这是一个缓慢的操作),因此节省了大量时间。

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). 如果您想让Git使用某种占位符的索引条目来知道文件的存在,请考虑使用git add --indent-to-add ,或者简称为git add -N (大写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. 我还要提到的是这种状态(“假加”)是很长一段时间坏了,所以有许多的Git版本,它古怪的行为。 It was also just updated in 2.19 to correct the git diff output : 它也刚刚在2.19中更新,以更正git diff输出

  • "git diff" compares the index and the working tree. “ git diff”比较索引和工作树。 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. 现在默认情况下它们显示为新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM