Home > OS >  With using Git, `git status` will output paths of untracked files and directories. How do I get exac
With using Git, `git status` will output paths of untracked files and directories. How do I get exac

Time:09-25

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?

CodePudding user response:

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.

CodePudding user response:

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.)

  •  Tags:  
  • git
  • Related