How can I get git to list all untracked files recursively?
Asked Answered
Y

3

12

I am trying to get a list of untracked files to use programmatically.

However I can't seem to get git to report untracked files inside untracked directories. It always just lists the lowest level directory that contains files.

I've tried:

git status --untracked-files=all

and

git ls-files --others --exclude-standard

Staging any file in the directory makes git report all other files in the directory, but I don't really want to do that.

Also, I could use bash to list all the files - but then I would have to get git to tell me which of those are gitignored.

Is there any way to do this purely with git? Or otherwise an efficient way to do this with other tools?

Yasmin answered 14/8, 2018 at 19:29 Comment(6)
git status --untracked-files=all should be equivalent to git status -u, and that should show untracked files in directories where no other file is tracked. That still doesn't work for you?Insomniac
What Git version are you using? In 2.16.4, both git status --untracked-files=all and git ls-files --others --exclude-standard seem to give the results you desire, i.e. listing all the untracked but not ignored files individually.Metts
I'm on 2.15.2, so looks like that's the answer! Want to add it?Yasmin
Note that it should work this way in all versions of Git that support git status -uall and git ls-files --others --exclude-standard (both of which go back a long way)...Alexandria
I've upgraded to 2.18.0, but the behaviour is the same :(Yasmin
Okay, so it turns out this only happens for specific folders, but the permissions / group / owner are identical to another folder which doesn't do this. I wonder if there's some OSX specific settings?Yasmin
E
5

Git doesn't expand untracked folders which are also Git repositories

Those are called "nested Git repositories", and recorded in a Git repo as gitlinks, a special entry in the parent repo index.

Since they are an entry (a sort of special "file"), they would not be concerned with a -untracked-files=all.


Note, if you do use -untracked-files=all, use it with Git 2.38 (Q3 2022), which includes a fix for a bug that makes write-tree to fail to write out a non-existent index as a tree, introduced in 2.37.

See commit 4447d41 (22 Jul 2022) by Martin Ågren (none).
(Merged by Junio C Hamano -- gitster -- in commit f1a0db2, 03 Aug 2022)

read-cache: make do_read_index() always set up istate->repo

Reported-by: Joey Hess
Signed-off-by: Martin Ågren

If there is no index file, e.g., because the repository has just been created, we return zero early (unless must_exist makes us die instead.)

This early return means we do not set up istate->repo.
With core.untrackedCache=true, the recent e6a6535 ("untracked-cache: support '--untracked-files=all' if configured", 2022-03-31, Git v2.37 -- merge) will eventually pass down istate->repo as a null pointer to repo_config_get_string(), causing a segmentation fault.

If we do hit this early return, set up istate->repo similar to when we actually read the index.

Ewall answered 15/8, 2018 at 2:50 Comment(0)
Y
3

Finally got the answer: Git doesn't expand untracked folders which are also Git repositories.

Yasmin answered 14/8, 2018 at 23:43 Comment(0)
M
0

You can do

git ls-files --exclude-standard --others

This will list all untracked files recursively without formatting if you want to use it in a script or something.

Source: https://ploegert.gitbook.io/til/tools/git/list-untracked-files

Mandibular answered 31/7 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.