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.
7 Answers
The commands below no longer give the expected results, I may originally also have made some mistakes. Please double-check your output and the options you are using. Nevertheless: The basic tenet of using diff-filter should still hold true.
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
-
6
-
7It's quite unfortunate that the OP doesn't accept this answer, as in my humble opinion, you're completely right. :)– Jan NashFeb 13, 2016 at 16:34
-
3The first of the three commands is correct. The second doesn't make much sense, since a file is only marked as "new" when it has been staged. The third is wrong, as it doesn't even list files that are marked as "new" in the index. Jan 26, 2020 at 11:12
-
1@agemO, this used to work. I agree though, that this is complete BS now. (I added a note to the answer)– andsensAug 19, 2020 at 14:29
-
1I don't know why someone claims that "The third is wrong, as it doesn't even list files that are marked as "new" in the index", but the third command is working perfectly for me as of today, and it answers the exact question that OP asked for -- how to get the pure list of the
new file:
of thegit status
output, which is what I came here for.– xptJul 2, 2021 at 19:50
You can use git status --short to get the list of files.
-
13
-
The OP asked for how to get the pure list of the
new file:
of thegit status
output, just get him "the plain file names", but this one has an extra status leading character.– xptJul 2, 2021 at 19:54 -
1Fantastic! Just what I wanted! git ls-files --others wasn't working for me. With git status --short, I could just grep for "^??". Thanks!– ZeZNiQJul 15, 2021 at 15:19
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
-
1OP asked to list new files and this answer doesn't required any other programs to pipe the output to. I found on mac git diff didn't work with the filters as expected while this did :)– ted-k42May 17, 2017 at 23:14
-
This is clearly the winner. It works on new directories containing all new files.– AliJul 10, 2017 at 16:24
-
However, note that this doesn't work from subdirectories. I ended up using
git diff
to make my script work from every directory in the repository:$ git diff --name-only --diff-filter=A HEAD
Jan 8, 2018 at 10:19 -
1@AndreasSiegel, this is by design as it is generally more helpful to follow the behaviour of the ls-files command. Since the command follows the general convention of accepting qualifying arguments, a better solution for your alias would be to amend the alias as follows: <code>git ls-files -o --exclude-standard --full-name --
git rev-parse --show-toplevel
</code> Jan 23, 2018 at 15:11 -
@AndreasSiegel, this is by design as it is generally more helpful to follow the behaviour of the ls-files command. Since the command follows the general convention of accepting qualifying arguments, a better solution for your alias would be to amend the alias as follows: `git ls-files -o --exclude-standard --full-name -- ``git rev-parse --show-toplevel``` Jan 23, 2018 at 15:13
I would use something like git status --porcelain | grep "^A" | cut -c 4-
-
It's years later, but this no longer works for me (the output of
git status
has likely changed). EDIT: Bah. Hadn't added/committed it. Stands the test of time!– HendyAug 25, 2017 at 15:53 -
1To get both unstaged (untracked) and staged new files:
git status --porcelain | grep '^A\|^??' | cut -c 4-
Feb 3 at 15:18
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
....
git status | grep "new file:" | cut -c 14-
git status | grep "modified:" | cut -c 14-
To list All new files that is not added before to git project, you can use this command
git ls-files -o
The -o option is shorthand for --others
git status | grep new file
.git ls-files --others --exclude-standard
(stackoverflow.com/a/2299448/6309)