70

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.

4

7 Answers 7

111

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
8
  • 6
    diff-filter is actually the way to go :) Dec 26, 2015 at 9:19
  • 7
    It's quite unfortunate that the OP doesn't accept this answer, as in my humble opinion, you're completely right. :)
    – Jan Nash
    Feb 13, 2016 at 16:34
  • 3
    The 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)
    – andsens
    Aug 19, 2020 at 14:29
  • 1
    I 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 the git status output, which is what I came here for.
    – xpt
    Jul 2, 2021 at 19:50
45

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

3
  • 13
    This should not be overlooked.
    – jarederaj
    Jun 20, 2013 at 23:10
  • The OP asked for how to get the pure list of the new file: of the git status output, just get him "the plain file names", but this one has an extra status leading character.
    – xpt
    Jul 2, 2021 at 19:54
  • 1
    Fantastic! Just what I wanted! git ls-files --others wasn't working for me. With git status --short, I could just grep for "^??". Thanks!
    – ZeZNiQ
    Jul 15, 2021 at 15:19
37

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
6
  • 1
    OP 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-k42
    May 17, 2017 at 23:14
  • This is clearly the winner. It works on new directories containing all new files.
    – Ali
    Jul 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
32

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

2
  • 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!
    – Hendy
    Aug 25, 2017 at 15:53
  • 1
    To get both unstaged (untracked) and staged new files: git status --porcelain | grep '^A\|^??' | cut -c 4- Feb 3 at 15:18
1

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
 ....
0
git status | grep "new file:" | cut -c 14-

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

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

see this gif image list new files in git

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.