.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.
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' > .gitignoreCommand:
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.txtTE-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.txtCommand:
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.txtTE-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' > .gitignoreCommand:
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=0TE-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.txtCommand:
git check-ignore -q keep.txtRecorded 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.
Source
Evidence
TE-11-A: gitignore does not untrack a file that is already tracked; git add -f stages an ignored fileTE-11-B: a tracked file stays tracked even after it is added to .gitignoreTE-11-C: check-ignore shows which pattern matched, including a negation and a directory rule
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
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