git ls-files:如何识别新文件(已添加,未提交)?


73

在我调用git add <file>该命令后,git status将显示以下内容:

...
new file:    <file>

我不知道如何使用来管理它以获取相同的信息ls-filesls-files -tc在这种情况下)将显示给我:

H <commited file>
H <other commited file>
H <file>

似乎没有命令行开关可用于新文件。该文件报告为已缓存,可以,但是如何确定当前未提交该文件?

使用ls-files某些类似的命令(在我不必像那样分析很多输出的情况git status)下,这是否可行?

Answers:


91

您要使用git diff --cached。有了--name-only它,它将列出相对于HEAD索引中所有已更改的文件。使用,--name-status您还可以获取状态符号,使用--diff-filter可以指定要显示的文件集(例如,新添加的文件为“ A”)。使用-M开启移动侦测和-C如果你想让他们的拷贝检测。

为了最严格地阅读您写的内容,git diff --cached --name-only --diff-filter=A将列出HEAD之后添加的所有文件,这些文件在HEAD中不存在。


1
仅供参考:您的“最严格的阅读”不包括该--cached标志。
docwhat 2010年

3
为了您的复制乐趣:git diff --cached --name-only
aliteralmind 2014年

47

澄清:这是一种显示我想要的文件的方式添加。这不是OP所要的,但是我会保留此帖子,以防它对其他人有用。

这似乎仅显示我添加的文件(到我的工作副本,而不是索引),但与我的标准忽略模式不匹配:

 $ git ls-files --others --exclude-standard

如果没有--exclude-standard,它还会显示我运行时忽略的文件git status


问题是文件与文件中的规则匹配.gitignore,并添加了git add -f...,因此不会显示--exclude-standard。我想知道git status它的信息从哪里得到
tanascius'2

等等,我可能搞砸了-您是否仍需要添加文件,还是正在寻找索引中已经添加的新文件?
Mike Seplowitz 2010年

1
我正在寻找索引中尚未
提交

嗯,那我想安德鲁的答案更好。将编辑我的内容以澄清这一点。
Mike Seplowitz 2010年

1

我想查看所有添加和修改的文件,因此
git diff --cached --name-only
+的合并
git ls-files --modified --deleted
[1]+ Stopped ...之间没有合并。

当我使用时,git diff --cached --name-only && git ls-files --modified --deleted我收到:

content/blog/2020/05/artificial-intellect.json
content/blog/2020/05/artificial-intellect.pug

[1]+  Stopped                 git diff --cached --name-only
content/index.json
index.php
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.