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

在我调用 git add <file>之后,命令 git status会显示如下内容:

...
new file:    <file>

不知为何,我无法通过使用 ls-files获得相同的信息,它(在本例中为 ls-files -tc)将向我显示:

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

新文件似乎没有命令行开关。报告的文件是缓存的,这没有问题,但是我如何发现它在这个时候没有被提交?

使用 ls-files或类似的命令(不必像解析 git status那样解析大量输出)可以做到这一点吗?

135406 次浏览

Clarification: This is a way to show the files that I intend to add. This is not what the OP was looking for, but I'll leave this post in case it's useful to others.

This seems to show only the files that I have added [to my working copy, not the index] but aren't matched by my standard ignore patterns:

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

Without --exclude-standard, it also shows files that are ignored when I run git status.

You want to use git diff --cached. With --name-only it'll list all the files you've changed in the index relative to HEAD. With --name-status you can get the status symbol too, with --diff-filter you can specify which set of files you want to show ('A' for newly added files, for instance). Use -M to turn on move detection and -C for copy detection if you want them.

For the strictest reading of what you wrote, git diff --cached --name-only --diff-filter=A will list all the files you've added since HEAD which don't exist in HEAD.

I would like to see all the added and modified files, so the merge of
git diff --cached --name-only
+
git ls-files --modified --deleted
but without [1]+ Stopped ... in between.

When I use git diff --cached --name-only && git ls-files --modified --deleted I receive:

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

Simple way to get all new, modified and removed files:

git ls-files --others --modified --deleted --exclude-standard

Refer to official documentation: https://git-scm.com/docs/git-ls-files

Use "--others" if you wish to see new/untracked files

Use "--modified" if you wish to see modified files

Use "--deleted" if you wish to see deleted files

--exclude-standard will exclude the files specified in .gitignore