Tells you which .gitignore rule matched.
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.
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 '!'.
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.| Flag | What it does |
|---|---|
-v, --verbose | Print source <file>:<line>:<pattern> alongside each path. Use this 95% of the time. |
-n, --non-matching | Also show paths with no matching rule (with -v, source columns are blank). Lets you batch-classify a list. |
--no-index | Ignore the index when deciding. Lets you debug rules even if a path is already tracked (tracked files are normally exempt from being 'ignored'). |
--stdin | Read paths from stdin instead of argv. Pair with -z for NUL-delimited input from find -print0/ls-files -z. |
-z | NUL-terminate input (with --stdin) and output. Use whenever paths may contain newlines or spaces. |
-q, --quiet | Suppress output; just set the exit code. Perfect for shell if tests. Only one path allowed. |
check-ignore -q returns 0.git check-ignore -v build/output.ogit ls-files -o -z | git check-ignore -v -n --stdin -zgit check-ignore -q dist/ && echo skippinggit check-ignore -v --no-index docs/notes.mdHit each option, then Check answers. Score is recorded; Next is always open.