2

Example:

$ git --version
git version 2.33.0.windows.2

$ ls --recursive
.:
untracked-directory/  untracked-file.txt

./untracked-directory:
untracked-file-1-in-untracked-directory.txt  untracked-file-2-in-untracked-directory.txt

$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        untracked-directory/
        untracked-file.txt

nothing added to commit but untracked files present (use "git add" to track)

How do I get the paths in the "Untracked files" section of the output of git status?

slartar
  • 76
  • 4

2 Answers2

2

git ls-files --others --exclude-standard --directory[a] seems to always give the paths that git status gives for "Untracked files".

From the same example in the question body:

$ git --version
git version 2.33.0.windows.2

$ ls --recursive
.:
untracked-directory/  untracked-file.txt

./untracked-directory:
untracked-file-1-in-untracked-directory.txt  untracked-file-2-in-untracked-directory.txt

$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        untracked-directory/
        untracked-file.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git ls-files --others --exclude-standard --directory
untracked-directory/
untracked-file.txt

Note

[a] git ls-files --others --exclude-standard --directory was adapted from git ls-files --others --exclude-standard of the accepted answer of "Git: list only "untracked" files (also, custom commands)" by trying out git ls-files --others --exclude-standard from that answer, noticing it wasn't the exact paths that git status reports, and then somehow I got to the documentation for git ls-files, searched the page for "directory", and then I found the --directory option, and then I tried it out, and it gave the paths that git status gives, and I have yet to find an example where both git status and git ls-files --others --exclude-standard --directory disagree.

slartar
  • 76
  • 4
  • 1
    Just for fun, I tracked down [the last version of git where `git-status` was still a shell script](https://github.com/git/git/blob/v1.1.6/git-status.sh) and sure enough, it used `git-ls-files -z --others --directory --exclude-per-directory=.gitignore` (the `-z` is just formatting with zero-bytes for easier post-processing in the rest of the script). There's no guarantee things haven't changed in the following 15 years to introduce new edge-cases, though! – IMSoP Sep 23 '21 at 20:40
  • @IMSoP This is fun! I'm wondering if it possible to find things in the source of current Git to add sufficient confidence that `git ls-files --others --exclude-standard --directory`'s output and `git status`'s "Untracked files" output will always be the same. Based on `git ls-files -h` describing `--directory` with "show 'other' directories' names only", which lead to [this search of Git's source](https://github.com/git/git/search?q=%22show+%27other%27+directories%27+names+only%22), which lead to [continued in future comment] – slartar Sep 23 '21 at 22:22
  • [continued from past comment] [this search](https://github.com/git/git/search?q=DIR_SHOW_OTHER_DIRECTORIES), which presented both of the files [wt-status.c](https://github.com/git/git/blob/99c99ed8259bf070cd8ae7b51a94904b7cf5c161/wt-status.c) and [ls-files.c](https://github.com/git/git/blob/99c99ed8259bf070cd8ae7b51a94904b7cf5c161/builtin/ls-files.c), it seems that perhaps it might be possible to find one or more similarities, between ls-files.c and wt-status.c, that will give the earlier desired confidence I mentioned. At this moment I haven't found any clear confidence booster. – slartar Sep 23 '21 at 22:22
1

git status -uall will show all the files, rather than summarizing them. Note that this requires the word all to be joined to the -u part.

This works even in quite old versions of Git. Newer Git versions have a status.showUntrackedFiles setting so that you can set this to default to showing all files. Note that it takes longer to show all files: when Git can abbreviate like this, Git can short-circuit the work it would have to do to find all the file names. (How much longer is very working-tree and OS and file system dependent.)

torek
  • 448,244
  • 59
  • 642
  • 775