peterjwest
peterjwest

Reputation: 4452

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

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?

Upvotes: 13

Views: 7629

Answers (3)

sakana-boy
sakana-boy

Reputation: 35

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

Upvotes: 1

VonC
VonC

Reputation: 1323633

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.

Upvotes: 5

peterjwest
peterjwest

Reputation: 4452

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

Upvotes: 3

Related Questions