Sub topic 9
Finding out why a file is ignored
This chapter assumes you know the basic Git workflow (git init, git add, git commit, git status) and the pattern syntax from earlier chapters: wildcards, directory markers, anchoring, **, and negation. You will learn to find which rule ignores a file, or why a rule you expected to work does not.
The problem
A file does not show up in git status, or git add refuses it. Several things could be responsible:
- A pattern in a
.gitignorein the file's directory or in a parent directory. - A pattern in
.git/info/exclude. - A pattern in the global file set by
core.excludesFile.
Reading every ignore file by hand is slow. Git can report the matching rule for you.
Using git check-ignore -v
The -v (verbose) flag makes git check-ignore print the source of the rule that matches a path.
git check-ignore -v build/output.logThe output has this format:
<source>:<line number>:<pattern><TAB><path>For example:
.gitignore:3:*.log build/output.logThis says line 3 of .gitignore contains *.log, and that pattern ignores build/output.log. If the rule comes from another source, the first field shows that file, such as .git/info/exclude or the path of your global ignore file.
If the path is not ignored, the command prints nothing and exits with status 1. A match exits with status 0. Scripts can use this exit status.
git check-ignore -q build/output.log && echo "ignored" || echo "not ignored"The -q flag suppresses output and leaves only the exit status.
Checking paths that do not exist yet
git check-ignore works on paths as strings, so the file does not need to exist. You can test a rule before you create the file.
git check-ignore -v dist/app.js node_modules/pkg/index.js README.mdEach ignored path gets its own line. Paths that are not ignored are left out.
Seeing negated rules
By default, git check-ignore reports only paths that end up ignored. Add -n (--non-matching) together with -v to list every path you ask about. A path that no rule matches is shown with empty source fields.
git check-ignore -v -n logs/keep.txt notes.txtA negation pattern that matches is also shown in the output. The line looks like .gitignore:4:!logs/keep.txt. This tells you the file was re-included on purpose.
Why a rule seems not to work
Run git check-ignore -v first. Then compare the result with the usual causes in the table.
| Symptom | Likely cause | What to check |
|---|---|---|
| A file is ignored and you did not expect it | A broad pattern such as `*.log` or a pattern in a parent directory | The source and line in the `-v` output |
| A file is not ignored although a rule exists | The file is already tracked | `git ls-files path` |
| A `!` rule has no effect | A parent directory is ignored | Look for a pattern like `logs/` |
| A pattern does not match nested files | The pattern is anchored with a leading `/` or contains a middle `/` | Whether you need `**/` |
| Rule works in one directory only | The rule is in a nested `.gitignore` | The location of the file that holds the rule |
The diagram shows the order of questions to ask.
A worked example
Suppose you want to keep logs/keep.txt, and you wrote this:
logs/
!logs/keep.txtgit status still does not show logs/keep.txt. Ask Git why:
git check-ignore -v logs/keep.txt.gitignore:1:logs/ logs/keep.txtThe match is line 1, the directory rule. Git never looks inside an ignored directory, so line 2 is never reached. Change the rules to ignore the contents instead of the directory:
logs/*
!logs/keep.txtRun the same command again. The output now names line 2, the negation, which confirms the file is re-included.
git check-ignore -v --no-index config/local.jsonExercise
You added logs/ and !logs/keep.txt to .gitignore, but logs/keep.txt is still ignored. Which command shows the rule responsible, and how do you fix the rules?
Show answer
Run git check-ignore -v logs/keep.txt. It reports the logs/ line, because the parent directory is ignored and Git never reads the negation. Replace logs/ with logs/* and keep !logs/keep.txt.
Exercise
A file named config/local.json is listed in .gitignore, yet git check-ignore -v config/local.json prints nothing and the file appears in git status as modified. What is the likely cause, and how do you confirm the rule itself is correct?
Show answer
The file is already tracked, so Git does not apply ignore rules to it. Run git check-ignore -v --no-index config/local.json to test the pattern alone. To stop tracking the file, use git rm --cached config/local.json.