KKAIROS
Practical guideIllustrative4 min read

.gitignore patterns: which files Git treats as untracked

.gitignore lists patterns for files Git should treat as untracked. It does not untrack a file that is already committed; git add -f stages an ignored file anyway. git check-ignore -q FILE prints nothing and gives only the exit status: 0 when the path is ignored, 1 when it is not. git check-ignore -v FILE prints the rule that decided it; with a negated rule it prints the rule and exits 0, so the printed rule, not the exit code, tells you the outcome.

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-11-A: gitignore does not untrack a file that is already tracked; git add -f stages an ignored file

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; printf 'b\n' > ignored.log; git add tracked.txt; git commit -qm one
printf '*.log\n' > .gitignore

Command:

git add .gitignore; git commit -qm gi >/dev/null; echo ignored_shown=$(git status --porcelain ignored.log | wc -l); echo tracked_shown=$(git status --porcelain tracked.txt | wc -l); git add -f ignored.log; echo forced=$(git diff --cached --name-only | grep -c ignored.log); echo listed=$(git ls-files tracked.txt | wc -l); printf 'a2\n' >> tracked.txt; echo status=$(git status --porcelain tracked.txt)

Recorded output:

ignored_shown=0
tracked_shown=0
forced=1
listed=1
status= M tracked.txt

TE-11-B: a tracked file stays tracked even after it is added to .gitignore

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\ntracked.txt\n' > .gitignore; git add .gitignore; git commit -qm gi
printf 'b\n' >> tracked.txt

Command:

git ls-files tracked.txt; echo listed=$(git ls-files tracked.txt | wc -l); echo status=$(git status --porcelain tracked.txt)

Recorded output:

tracked.txt
listed=1
status= M tracked.txt

TE-11-C: check-ignore shows which pattern matched, including a negation and a directory rule

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
mkdir -p sub dir; printf 'x\n' > a.log; printf 'x\n' > sub/b.log; printf 'x\n' > keep.log; printf 'x\n' > dir/x; printf 'x\n' > root-only
printf '*.log\n!keep.log\ndir/\n/root-only\n' > .gitignore

Command:

for f in a.log sub/b.log keep.log dir/x root-only; do git check-ignore -v $f; echo rc=$?; done; git check-ignore -q keep.log; echo q_keep=$?; git check-ignore -q a.log; echo q_a=$?

Recorded output:

.gitignore:1:*.log	a.log
rc=0
.gitignore:1:*.log	sub/b.log
rc=0
.gitignore:2:!keep.log	keep.log
rc=0
.gitignore:3:dir/	dir/x
rc=0
.gitignore:4:/root-only	root-only
rc=0
q_keep=1
q_a=0

TE-11-D: expected non-zero exit: check-ignore -q exits 1 when the path matches no ignore rule

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 '*.log\n' > .gitignore
printf 'x\n' > keep.txt

Command:

git check-ignore -q keep.txt

Recorded output: none, standard output was empty.

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

TE-11-D: by the setup, .gitignore holds only the pattern *.log. git check-ignore -q keep.txt printed nothing and exited with status 1.

Limits

The runs here show tracked versus ignored status and four pattern forms: a wildcard, a negation, a directory rule and a root-anchored rule. Other forms are not demonstrated on this page. git add -f overrides an ignore rule.

Ad

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-11-A | gitignore does not untrack a file that is already tracked; git add -f stages an ignored file | 0 | PASS | f1d8ad9c1a25 | | TE-11-B | a tracked file stays tracked even after it is added to .gitignore | 0 | PASS | 106076df9cfe | | TE-11-C | check-ignore shows which pattern matched, including a negation and a directory rule | 0 | PASS | bdbbdb489140 | | TE-11-D | expected non-zero exit: check-ignore -q exits 1 when the path matches no ignore rule | 1 | PASS | c703ee774928 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides