git ls-files Command: List Tracked Files in CI
git ls-files prints the files Git knows about in the index, with filters for state.
When a CI step needs to operate on exactly the files Git tracks (and skip vendored or ignored ones), ls-files is faster and safer than find because it already respects .gitignore.
Common flags
(default)- list cached (tracked) files-m/--modified- list files modified in the working tree-o --exclude-standard- list untracked files honoring .gitignore-d/--deleted- list deleted files-z- NUL-separate output for safe handling of unusual filenames-- <pathspec>- restrict to matching paths
Example
shell
# Lint only tracked shell scripts, NUL-safe
git ls-files -z -- '*.sh' | xargs -0 -r shellcheckIn CI
ls-files is the reliable way to enumerate tracked files for linters and formatters because it ignores build artifacts and gitignored paths automatically. Pair -z with xargs -0 so filenames with spaces do not break the pipeline.
Key takeaways
- git ls-files lists only tracked files, automatically skipping ignored artifacts.
- Use -z with xargs -0 to handle unusual filenames safely.
- It is a cleaner input for linters than find, which does not know about .gitignore.
Related guides
git status Command: Check Tree State in CIgit status shows working tree and index state. Reference for --porcelain, the stable machine-readable format…
git diff Command: Detect Changes in CIgit diff shows changes between commits or the working tree. Reference for --name-only, --stat, and --exit-cod…
git update-index Command in CIgit update-index manipulates the staging area directly. Reference for --assume-unchanged, --chmod, and --refr…