Skip to content
shellhacks.dev
Git

List untracked files only

List untracked files only with git ls-files -o --exclude-standard. Without the second option the output includes every ignored build file as well.

1 min read
Summarize in AI

You want the files Git does not know about yet, without the build output a .gitignore already hides.

Shell
git init -q demo && cd demo
printf 'build/\n*.log\n' > .gitignore
mkdir build && touch build/app.o debug.log notes.txt tracked.txt
git add tracked.txt .gitignore
git ls-files -o
echo ---
git ls-files -o --exclude-standard
Text
build/app.o
debug.log
notes.txt
---
notes.txt

-o (--others) lists files that are not in the index, and --exclude-standard applies the usual ignore rules: .gitignore, .git/info/exclude and the global ignore file. The options are in the git-ls-files manual. The first list shows why the second option matters, since the ignored directory and log appear without it.

Directories that hold only untracked files are listed file by file, unless you add --directory. File names with unusual characters are quoted in the output, so use -z with xargs -0 when feeding the list to another command.

Share

More snippets

Git

Show the current branch name

Print the current branch name with git branch --show-current, which prints nothing on a detached HEAD, where rev-parse --abbrev-ref prints the word HEAD.

1 min read
Git

Show the repository root

Print the root of the working tree with git rev-parse --show-toplevel, from any subdirectory, and cd to it in a script that must run from the top.

1 min read
Git

Check for uncommitted changes in a script

Test for uncommitted changes in a script with git diff --quiet, which exits 1 on a change. Add --cached for staged work, since untracked files never count.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.