git: list new files only

≡放荡痞女 提交于 2019-12-29 12:26:22

问题


When I do a git status I 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.


回答1:


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




回答2:


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 diff to show the changes. --name-only shows only the filenames. --diff-filter=A lists 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:

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



回答3:


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




回答4:


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

git ls-files -o  --exclude-standard

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

You may wish to include the --full-name option:

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



回答5:


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 ??

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



回答6:


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

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


来源:https://stackoverflow.com/questions/9000163/git-list-new-files-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!