Sub topic 8
Precedence and nested .gitignore files
This chapter assumes you can run git init, git add, git commit, and git status, and that you know how to write basic patterns, directory patterns, and ! negations. Here you will learn which rule wins when several rules match the same path.
Where ignore rules come from
Git reads ignore patterns from several places. From highest to lowest priority:
- Patterns given on the command line, such as with
git add --exclude. These are rarely used. - The
.gitignorefile in the same directory as the path, then in each parent directory up to the repository root. Files closer to the path win. - The repository's
.git/info/excludefile. - The file named by
core.excludesFile, the global ignore file.
Two things set this order apart from what many people expect. Priority applies between sources, and also between lines inside one file.
Last match wins within a file
Inside a single file, Git checks patterns in order and the last matching pattern decides the outcome. That is why a ! line must come after the pattern it overrides.
*.log
!important.logHere important.log matches both lines. The last one is the negation, so the file is not ignored. Reverse the order and *.log matches last, so important.log is ignored again.
Nested .gitignore files
You can place a .gitignore in any subdirectory. Its patterns apply only to that directory and everything below it. Patterns with a slash are relative to the directory that holds the file, not to the repository root.
Consider this layout:
project/
├── .gitignore
├── app.log
└── docs/
├── .gitignore
├── build.log
└── notes.logThe root file ignores all logs:
*.logThe nested file in docs/ re-includes one of them:
!notes.logdocs/.gitignore sits closer to docs/notes.log, so its rule beats the root rule. The result is that app.log and docs/build.log are ignored, and docs/notes.log is not.
A nested file cannot override a parent directory rule
The parent directory limitation from the negation chapter still applies. If a rule ignores a whole directory, Git never reads the .gitignore inside it.
# root .gitignore
docs/A docs/.gitignore containing !notes.log has no effect here, because Git skips docs/ entirely. To use a nested override, ignore the directory's contents instead of the directory:
# root .gitignore
docs/*
!docs/.gitignore
!docs/notes.logChecking the result
When two files disagree, ask Git which rule matched instead of guessing.
git check-ignore -v docs/notes.log docs/build.logThe output lists the source file, line number, and pattern for each path. For a path that a negation re-included, add -n to also print non-matching paths. A negated match is shown with its ! pattern.
Common errors
| Symptom | Likely cause |
|---|---|
| A `!` line has no effect | It appears before the broader pattern, or a parent directory is ignored |
| A nested rule is ignored | A higher rule excludes the directory itself |
| A path pattern in a subdirectory file matches nothing | The path was written relative to the repository root instead of the nested file |
| A personal rule hides a file for teammates | It lives in `.git/info/exclude` or the global file, which are not shared |
Exercise
The root .gitignore contains *.log. The file src/.gitignore contains !debug.log. Is src/debug.log ignored? What if the root file instead contains src/ on its own line?
Show answer
In the first case it is not ignored, because the nested file is closer to the path and its negation overrides the root pattern. In the second case it is ignored, because the root rule excludes the src/ directory, so Git never reads src/.gitignore.