Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git check-ignore command

Reference for git check-ignore: find out whether a path is ignored, which pattern and file matched with -v, test with --no-index, and read the exit codes.

git check-ignore answers the question "is this path ignored, and why?". Given one or more paths, it prints those that are ignored. With -v it also shows the file, line number and pattern responsible, which makes it the first tool to reach for when a file refuses to be added or an unexpected file is hidden.

The command evaluates all the sources Git uses: .gitignore files in the tree, .git/info/exclude and the global file named by core.excludesFile.

Terminal
git check-ignore build/app.o
git check-ignore -v build/app.o

Try it

Bash Which rule ignores this file?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Tracked files and --no-index; exclude and stdin

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git check-ignore [<options>] <pathname>...
git check-ignore [<options>] --stdin
Name Description
<pathname>... Paths to test, relative to the current directory. They do not have to exist.
-v, --verbose Print <source>:<linenum>:<pattern><TAB><pathname> for each match.
-n, --non-matching With -v, also show paths that match nothing (the source and pattern columns are empty).
--no-index Ignore the index: report matching rules even for tracked files.
--stdin Read paths from standard input, one per line.
-z Use NUL as separator for input and output.
-q, --quiet Print nothing; use only the exit code. Allowed with exactly one path.

Returns: the ignored paths (or the verbose form).

Exit codes: 0 if at least one path is ignored, 1 if none is ignored, 128 for a fatal error.

Usage notes

  • Tracked files are skipped. A file already in the index is never "ignored" in practice, so check-ignore prints nothing for it unless you pass --no-index. This is the usual reason a pattern "does not work": the file was committed earlier. Remove it with git rm --cached.
  • Negation is shown. A matching negated pattern (!keep.log) appears in -v output with the leading !, so you can see the file is explicitly un-ignored. Without -v it is not printed.
  • Directories: a pattern ending in / only matches directories, so test build/ or a path inside it, not a file named build.
  • Last match wins within one source, and files closer to the path override those further away. -v reports the deciding line.
  • Not for untracked listing. To see all ignored files use git status --ignored or git ls-files -o -i --exclude-standard.
  • Case: matching follows core.ignoreCase (see core.ignoreCase).