git:只列出新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9000163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:49:58  来源:igfitidea点击:

git: list new files only

gitstatus

提问by BetaRide

When I do a git statusI get a list of files prefixed with new file:. How can I get only this list? I want to process this files in a simple loop in a little shell script.

当我执行 a 时,git status我会得到一个以new file:.为前缀的文件列表。我怎样才能只得到这个列表?我想在一个小的 shell 脚本中以一个简单的循环来处理这些文件。

采纳答案by Rom1

I would use something like git status --porcelain | grep "^A" | cut -c 4-

我会使用类似的东西 git status --porcelain | grep "^A" | cut -c 4-

回答by andsens

Don't use grep to parse git output. Git almost certainly has the things you are looking for built-in (except if you are going for really advanced stuff).
You can use git diffto show the changes. --name-onlyshows only the filenames. --diff-filter=Alists only the added files.
If you want to see new files you have already added to the index use --cached, otherwise omit it. To see both diff to HEAD.
The commands look like this:

不要使用 grep 来解析 git 输出。Git 几乎肯定有你正在寻找的内置东西(除非你想要真正高级的东西)。
您可以使用git diff来显示更改。--name-only只显示文件名。--diff-filter=A仅列出添加的文件。
如果您想查看已添加到索引中的新文件,请使用--cached,否则将其省略。要查看两者的差异HEAD
命令如下所示:

git diff --name-only --diff-filter=A --cached # All new files in the index  
git diff --name-only --diff-filter=A          # All files that are not staged  
git diff --name-only --diff-filter=A HEAD     # All new files not yet committed

回答by vpatil

You can use git status --shortto get the list of files.

您可以使用git status --short来获取文件列表。

回答by Keith Hanlan

Instead of parsing the output of "git status", it is probably more elegant to use

而不是解析“git status”的输出,使用起来可能更优雅

git ls-files -o  --exclude-standard

As with git status, ls-fileswill produce output relative to your currrent working directory.

git status,ls-files将产生相对于您当前工作目录的输出。

You may wish to include the --full-nameoption:

您可能希望包括以下--full-name选项:

git ls-files -o  --exclude-standard --full-name

回答by SuperNova

List of new files or to get files to be added to git, can be got using git status and grep command like git status -s | grep ??

新文件列表或获取要添加到 git 的文件,可以使用 git status 和 grep 命令获取,例如 git status -s | grep ??

root@user-ubuntu:~/project-repo-directory# git status -s | grep ??
 ?? src/.../file1.js
 ?? src/.../file2.js
 ?? src/.../file3.js
 ....

回答by Raviv Shweid

git status | grep "new file:" | cut -c 14-

git status | grep "modified:" | cut -c 14-