git:仅列出新文件

时间:2012-01-25 09:15:30

标签: git status

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

6 个答案:

答案 0 :(得分:75)

不要使用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

答案 1 :(得分:18)

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

的内容

答案 2 :(得分:16)

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

答案 3 :(得分:13)

不是解析" git status"的输出,而是使用它可能更优雅

git ls-files -o  --exclude-standard

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

您可能希望包含--full-name选项:

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

答案 4 :(得分:0)

新文件列表或获取要添加到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
 ....

答案 5 :(得分:0)

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

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