Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I find out which Git ignore rule matches a file?

Quick answer

From inside the repository, run git check-ignore -v -- path/to/file. Git reports the matching pattern and where it came from, including a per-directory exclude file, .git/info/exclude, or the file configured by core.excludesFile. If the file is tracked, the default check does not report it; add --no-index to check tracked files too. No match means these rules do not ignore that path.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a file that Git seems to be ignoring. What command tells me exactly which rule is responsible?

Geez

Run this from within the repository, replacing the example path with the path you want to investigate:

git check-ignore -v -- path/to/file

The -v option asks Git to show the matching pattern and its source, including the source line number and the path being checked. This is more useful than only asking whether a path is ignored, because it points you to the rule to inspect. [S1]

For example, imagine you check build/output.log and get an illustrative result like:

[source file]:[line]:[pattern] build/output.log

That layout is schematic, not output from a real repository. Read the source-file field first, then inspect the indicated rule.

Follow-up question

How can I tell whether the rule is in this repository or in a more general ignore file?

Geez

Look at the source path shown by the verbose command. Git may identify a per-directory exclude file, the repository’s .git/info/exclude, or the file configured by core.excludesFile; when the configured excludes file is the source, Git shows its absolute path. [S1]

That distinction helps you decide where to investigate: a per-directory source is associated with the relevant directory, while .git/info/exclude and the configured excludes file are other possible sources. The output identifies the source; it does not, by itself, tell you why someone chose that pattern or whether you should remove it. If you share the result for help, redact private path components first.

Follow-up question

I ran the command but got no result. Could the file still be ignored?

Geez

First check whether the file is already tracked. By default, git check-ignore does not report tracked files. To check a tracked path anyway, use:

git check-ignore -v --no-index -- path/to/file

If the path is not tracked and there is still no match, the checked path is not ignored by the ignore rules being examined. To display non-matching paths as well, add -n alongside -v, for example: git check-ignore -v -n -- path/to/file. [S1]

Keep the exact path you tested in your notes. A result for one path does not establish that a differently named file or a file in another directory is treated the same way.

Follow-up question

Can you give me a small workflow I can reuse when troubleshooting a file?

Geez

Use this sequence:

  1. From the repository, run git check-ignore -v -- path/to/file.
  2. If there is a match, note the printed pattern, source, and line number; inspect that rule in the named source.
  3. If there is no result, check whether the path is tracked. For a tracked path, rerun with --no-index.
  4. If you want a visible non-match result, use -n with -v.
  5. Record the path and command so another person can reproduce the check.

Copyable note:

Path checked: [repository-relative path] Command: git check-ignore -v -- [path] Result: [matching pattern and source, or no match] Tracked-file check needed: [yes/no]

This workflow identifies what Git reports for the path you tested. It does not establish how other tools treat the file or whether changing an ignore rule is appropriate.

A practical checklist

  • Run git check-ignore -v -- [path] from inside the repository.

  • Record the reported pattern, source file, and line number.

  • If there is no result, determine whether the path is tracked.

  • For a tracked path, rerun the check with --no-index.

  • Use -n with -v if you want non-matching paths shown.

A few more useful answers

What does the source path in verbose output tell me?

It identifies where Git found the matching rule, such as a per-directory exclude file, .git/info/exclude, or the configured core.excludesFile. The configured excludes file is shown with an absolute path. [S1]

Does no output prove that no ignore rule can ever apply to this file?

It means no matching rule was reported for the checked path under that check. Tracked files are omitted by default, so check those with --no-index; -n with -v can show a non-match. [S1]

Sources

References used for this guide.

  1. [S1] Git check-ignore documentation: identify matching ignore rules
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work