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.

BetaRide
  • 16,207
  • 29
  • 99
  • 177

7 Answers7

110

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
andsens
  • 6,716
  • 4
  • 30
  • 26
  • 6
    diff-filter is actually the way to go :) – Albus Dumbledore Dec 26 '15 at 09: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 '16 at 16:34
  • Is there something similar for "All files since the branch was created"? – Matt R Jun 03 '19 at 14:27
  • @MattR there is. The diff command takes two commit hashes as arguments. So presumably you want to diff from your current HEAD (1st arg). To find the the commit where your current HEAD and another branch diverge use `merge-base` like so: `git merge-base HEAD other-branch` Put the result into the original diff command: `git diff --name-only --diff-filter=A HEAD "$(git merge-base HEAD other-branch)"` – andsens Jul 26 '19 at 08:04
  • 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. – Peter John Acklam Jan 26 '20 at 11:12
  • Am I missing something or is it juste completely wrong, as @PeterJohnAcklam said ? – agemO Aug 18 '20 at 08:04
  • 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 '20 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 02 '21 at 19:50
45

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

vpatil
  • 3,380
  • 1
  • 17
  • 9
  • 13
    This should not be overlooked. – jarederaj Jun 20 '13 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 02 '21 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 '21 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
Keith Hanlan
  • 768
  • 7
  • 13
  • 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 '17 at 23:14
  • This is clearly the winner. It works on new directories containing all new files. – Ali Jul 10 '17 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` – Andreas Siegel Jan 08 '18 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: git ls-files -o --exclude-standard --full-name -- `git rev-parse --show-toplevel` – Keith Hanlan Jan 23 '18 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``` – Keith Hanlan Jan 23 '18 at 15:13
  • As of git version 2.39.2, this doesn't include any of the "new file"s output by `git status`. It lists only untracked new files. – Dan Dascalescu Jun 01 '23 at 22:22
32

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

Rom1
  • 3,167
  • 2
  • 22
  • 39
  • 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 '17 at 15:53
  • 1
    To get both unstaged (untracked) and staged new files: `git status --porcelain | grep '^A\|^??' | cut -c 4-` – Sihad Begovic Feb 03 '23 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
 ....
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0
git status | grep "new file:" | cut -c 14-

git status | grep "modified:" | cut -c 14- 
Sagar V
  • 12,158
  • 7
  • 41
  • 68
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

Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34