Progress:
TIER 2 · MODULE 41· Intermediate

git check-ignore

Tells you which .gitignore rule matched.

🎯 What & why

Tells you which .gitignore rule (and which file, which line) is causing a path to be excluded. The first thing to reach for when 'why won't git add this?' strikes.

🧠 Mental model

It walks the same exclude-matching machinery git itself uses: command-line excludes, per-directory .gitignore, .git/info/exclude, and the global core.excludesFile, in order. First match wins; later patterns can re-include with a leading '!'.

🛠️ Synopsis

git check-ignore [<options>] <pathname>...
git check-ignore [<options>] --stdin

Reports which gitignore pattern (if any) excludes each path. Exit code
encodes the answer: 0 = at least one path is ignored, 1 = none are,
128 = a real error occurred. Quiet/verbose flags change output, not
status.

🎚️ Switches & options

FlagWhat it does
-v, --verbosePrint source <file>:<line>:<pattern> alongside each path. Use this 95% of the time.
-n, --non-matchingAlso show paths with no matching rule (with -v, source columns are blank). Lets you batch-classify a list.
--no-indexIgnore the index when deciding. Lets you debug rules even if a path is already tracked (tracked files are normally exempt from being 'ignored').
--stdinRead paths from stdin instead of argv. Pair with -z for NUL-delimited input from find -print0/ls-files -z.
-zNUL-terminate input (with --stdin) and output. Use whenever paths may contain newlines or spaces.
-q, --quietSuppress output; just set the exit code. Perfect for shell if tests. Only one path allowed.

💡 Use cases

🧪 Examples

Show the rule and source
git check-ignore -v build/output.o
Classify many paths
git ls-files -o -z | git check-ignore -v -n --stdin -z
Test in a script
git check-ignore -q dist/ && echo skipping
Probe a tracked path
git check-ignore -v --no-index docs/notes.md

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.