Explain which gitignore rule is ignoring my file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If Git is ignoring a file and you want to know exactly why, the right tool is not manual inspection of every .gitignore file. Git can tell you the matching rule directly, including the source file and line number.
The command to remember is git check-ignore -v. It is the fastest way to explain which ignore rule matched a path.
Use git check-ignore -v
Run the command with the file path you care about:
Example output looks like this:
That line tells you:
- the ignore source file
- the line number
- the exact pattern that matched
- the path you asked about
This is much better than guessing which pattern won.
Git Can Ignore from Several Places
The matching rule may come from more than one location. Common sources are:
- a repository
.gitignore - a nested
.gitignorein a subdirectory - '
.git/info/exclude' - a global excludes file configured in Git settings
That is another reason the command matters. The active rule might not be in the .gitignore file you were staring at.
Check a Directory or Several Files
You can ask about multiple paths at once:
This is useful when you are debugging a group of files and want to see whether one pattern or several different rules are involved.
Tracked Files Are Different
A common confusion is that .gitignore affects only untracked files. If a file is already tracked, adding it to .gitignore later will not make Git stop tracking it.
If that happens, remove it from the index first:
Then the ignore rule can take effect for future status checks.
Negation Rules Matter Too
Git ignore rules can be negated with !. That means a later rule can re-include something that an earlier rule excluded.
For example:
In cases like this, git check-ignore -v helps you confirm which final rule actually controlled the result. Ignore debugging is really precedence debugging.
Directories Can Hide Files Indirectly
Sometimes the file itself does not have a matching pattern, but one of its parent directories does. For example, if build/ is ignored, then build/output/report.txt is ignored because the directory is ignored. That is another reason to ask Git directly instead of scanning for the filename only. The effective rule may target a directory pattern rather than the exact file path.
Once you know the source line, the fix is usually obvious: remove the rule, narrow the pattern, or add a negation rule if the file should be re-included.
That feedback loop is much faster than editing several ignore files blindly.
Common Pitfalls
- Scanning
.gitignoremanually instead of usinggit check-ignore -v. - Forgetting that ignore rules can come from global excludes or
.git/info/exclude. - Expecting
.gitignoreto hide files that are already tracked. - Missing the effect of later negation rules such as
!important.log. - Debugging from the wrong working directory and passing the wrong relative path.
Summary
- Use
git check-ignore -v path/to/fileto see exactly which ignore rule matched. - Git reports the source file, line number, and pattern.
- Ignore rules can come from repository, nested, local, or global sources.
- '
.gitignoredoes not automatically stop tracking files already in the index.' - Most ignore-rule mysteries become obvious once you use the built-in command.
Related reading
- Explanation of Merge Sort for Dummies
- Export a stash to another computer
- Failed to pull image pull access denied , repository does not exist or may require ''docker login''
- Fast-forward merge is not possible. To merge this request, first rebase locally
- ExpressJS - Elastic Beanstalk 502 Bad Gateway
- Extension exists but uuid_generate_v4 fails
- fatal early EOF fatal index-pack failed
- fatal Not a git repository or any of the parent directories .git
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.