KKAIROS
Practical guideIllustrative5 min read

Git Ls Files: list what Git tracks

git ls-files lists the files in the index, which are the tracked files. --others lists untracked files, and --exclude-standard hides the ones that the standard ignore rules match (.gitignore, .git/info/exclude and the global ignore file).

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-90-A: ls-files lists tracked files; --others lists untracked files including an ignored one; adding --exclude-standard hides the ignored one

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > tracked.txt; git add tracked.txt; git commit -qm one
printf 'b\n' > new.txt; printf 'c\n' > skip.log; printf '*.log\n' > .gitignore

Command:

git ls-files; echo --others:; git ls-files --others; echo --others-exclude-standard:; git ls-files --others --exclude-standard

Recorded output:

tracked.txt
--others:
.gitignore
new.txt
skip.log
--others-exclude-standard:
.gitignore
new.txt

Exit status: 0. The output matches the expected text written before the run.

TE-90-B: with a committed .gitignore, --others --exclude-standard lists the new file and --ignored lists the ignored one

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > tracked.txt; git add tracked.txt; git commit -qm one
printf '*.log\n' > .gitignore; git add .gitignore; git commit -qm ign
printf 'b\n' > new.txt; printf 'c\n' > skip.log

Command:

git ls-files --others --exclude-standard; echo ignored:; git ls-files --others --ignored --exclude-standard

Recorded output:

new.txt
ignored:
skip.log

Exit status: 0. This status and the output match what was written before the run.

TE-90-C: expected non-zero exit: git ls-files with an option it does not have prints a usage error and exits with status 129

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one

Command:

git ls-files --nosuchoption

Recorded output: none, standard output was empty.

Standard error from the same run:

error: unknown option `nosuchoption'
usage: git ls-files [<options>] [<file>...]

    -z                    separate paths with the NUL character
    -t                    identify the file status with tags
    -v                    use lowercase letters for 'assume unchanged' files
    -f                    use lowercase letters for 'fsmonitor clean' files
    -c, --cached          show cached files in the output (default)
    -d, --deleted         show deleted files in the output
    -m, --modified        show modified files in the output
    -o, --others          show other files in the output
    -i, --ignored         show ignored files in the output
    -s, --stage           show staged contents' object name in the output
    -k, --killed          show files on the filesystem that need to be removed
    --directory           show 'other' directories' names only
    --eol                 show line endings of files
    --empty-directory     don't show empty directories
    -u, --unmerged        show unmerged files in the output
    --resolve-undo        show resolve-undo information
    -x, --exclude <pattern>
                          skip files matching pattern
    -X, --exclude-from <file>
                          read exclude patterns from <file>
    --exclude-per-directory <file>
                          read additional per-directory exclude patterns in <file>
    --exclude-standard    add the standard git exclusions
    --full-name           make the output relative to the project top directory
    --recurse-submodules  recurse through submodules
    --error-unmatch       if any <file> is not in the index, treat this as an error
    --with-tree <tree-ish>
                          pretend that paths removed since <tree-ish> are still present
    --abbrev[=<n>]        use <n> digits to display object names
    --debug               show debugging data
    --deduplicate         suppress duplicate entries

Exit status: 129. This status and the output match what was written before the run.

Reading the recorded output

The first list, before --others, holds one name, tracked.txt, the file that was added and committed. The --others list holds three names: .gitignore, new.txt and skip.log. In the last list, skip.log is gone, and .gitignore and new.txt are still there. By the setup, the .gitignore file contains the pattern *.log, which matches skip.log.

TE-90-B: by the setup, .gitignore is committed, so it is tracked. --others --exclude-standard listed only new.txt, and --others --ignored --exclude-standard listed skip.log, the file that *.log matches. TE-90-C: git ls-files --nosuchoption exited with status 129. The first line of standard error is error: unknown option `nosuchoption' and it is followed by the usage text.

Ad

Limits

In this run the .gitignore file itself is untracked, so it appears in both untracked lists. Only .gitignore was run as an ignore rule. Only a tiny repository was used. Options other than --others, --exclude-standard and --ignored were not run.

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-90-A | ls-files lists tracked files; --others lists untracked files including an ignored one; adding --exclude-standard hides the ignored one | 0 | PASS | 0122e7b33759 | | TE-90-B | with a committed .gitignore, --others --exclude-standard lists the new file and --ignored lists the ignored one | 0 | PASS | d331ffe548c7 | | TE-90-C | expected non-zero exit: git ls-files with an option it does not have prints a usage error and exits with status 129 | 129 | PASS | b2c445ca7522 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides