Use Git’s check-ignore to tell an ignored path from an exception to an ignore rule. In a script that gets a batch of files, read the rule attached to each path instead of treating the command’s status or every printed line as an ignored file.
Suppose a CI fixture directory has debug.trace, which should stay untracked, and sample.trace, a fixture that the rule should let through. Someone asks why the diagnostic reported both. Before adding another ignore rule, check what the leading exclamation mark means.
One verbose batch can contain all three verdicts.
A rule matched. Was the path excluded?
Make a disposable repository first, so the examples don’t touch your own files:
demo=$(mktemp -d)
cd "$demo"
git init -q
git config core.excludesfile /dev/null
printf '*.trace\n!sample.trace\n' > .gitignore
Those two rules mean:
*.trace
!sample.trace
Run git check-ignore -v -- sample.trace. On Git 2.56.1, the output names line 2 and prints !sample.trace; the command exits 0. git check-ignore -q -- sample.trace instead exits 1. The verbose form kept the exception for diagnosis. It is not an instruction to drop the file.
Same path and rules, with the terminal verdict changing between diagnostic and quiet modes.
If you only need one yes/no answer for an untracked path, use -q without -v. Interpret 0 as ignored, 1 as not ignored, and handle other statuses as errors. If a caller needs the rule for every path, read the records.
The later matching exception is the rule the verbose report prints.
Keep pathnames and rule fields intact
Use git check-ignore --stdin -z -v -n for the batch. Run it from the repository root and give it paths separated by NUL bytes; it returns four NUL-terminated fields per path: source, line, pattern, path. -n fills in rows for paths with no applicable rule, so a report can still show every input. Spaces or newlines inside a pathname don’t break a record.
A verbose NUL record carries the path and the evidence for its verdict.
import subprocess
def ignore_rules(paths):
paths = tuple(paths)
if any(not path or b"\0" in path for path in paths):
raise ValueError("expected non-empty paths without NUL")
request = b"".join(path + b"\0" for path in paths)
result = subprocess.run(
["git", "check-ignore", "--stdin", "-z", "-v", "-n"],
input=request, capture_output=True,
)
if result.returncode not in (0, 1):
raise RuntimeError(
result.stderr.decode("utf-8", errors="replace")
)
fields = result.stdout.split(b"\0")
if fields[-1] or len(fields) != 4 * len(paths) + 1:
raise ValueError("unexpected check-ignore output")
for i in range(0, len(fields) - 1, 4):
source, line, pattern, path = fields[i:i + 4]
if path != paths[i // 4]:
raise ValueError("unexpected output path")
if not pattern:
verdict = "no applicable rule in this check"
elif pattern.startswith(b"!"):
verdict = "kept by exception"
else:
verdict = "ignored"
yield path, verdict, source, line, pattern
paths = [b"build 1.trace", b"sample.trace", b"notes.txt"]
for row in ignore_rules(paths):
print(row)
The transcript shows the path and verdict columns from one run.
Status 0 or 1 tells the caller that Git answered. Neither labels the individual paths in the verbose report. A successful diagnostic can contain an ignored file, an exception and a non-match all at once. The code keeps paths as bytes, so a POSIX filename with an invalid UTF-8 byte still gets a verdict. Pass names like b"build 1.trace" and copy the path rather than splitting on spaces. The example buffers its finite batch in memory; keep batches bounded for very large repositories.
In the binary protocol, a line break inside a filename remains part of that one name.
An empty rule record is not proof the file is untracked
Git normally skips paths already in the index. If you stage saved.trace in the fixture with git add -f -- saved.trace, it gets an empty rule record in that batch, even with *.trace present.
The empty record alone can’t distinguish these cases.
To ask a different question, run git check-ignore -v --no-index -- saved.trace. You’ll see line 1 again. You’re testing which rule would apply without considering the index; Git has not stopped tracking the file. Use git ls-files --error-unmatch -- saved.trace to check the index directly.
Changing the diagnostic question doesn’t change the index.
That distinction makes it possible to answer three different bug reports without editing any files: a path was excluded, an exception let it through, or an existing index entry made the ignore rules irrelevant.
Validate the whole report before treating its rows as answers.
Sources
AI disclosure: Generative AI drafted this post; the commands were run in an isolated synthetic repository.









Top comments (0)