.gitignore order, re-includes, and the excluded-parent trap
In one line: Last match wins, why a `!` re-include stops working under an excluded directory, and safer exception patterns, with examples.
A .gitignore file is read from top to bottom, but the decision comes from the last rule that matches. Put a ! rule after an exclusion and the path comes back; put an exclusion after a re-include and it disappears again. The same list in a different order produces the opposite result.
The basic example
*.log !important.log logs/ !logs/keep.log
important.log survives through the second rule. logs/keep.log does not: the third rule excludes the logs directory itself, and git never descends into an excluded directory to build a file list, so the fourth rule is never examined. The debugger reports exactly this: “`!logs/keep.log` on line 4 is void because the parent `logs/` is excluded (line 3)”.
Two ways to open a directory again
First, re-include the directory and adjust inside it.
logs/ !logs/ logs/* !logs/keep.log
The directory is allowed, everything inside is excluded by default, and keep.log returns. Second, never exclude the directory at all; exclude at the child level.
logs/* !logs/keep.log
logs/* does not match the logs directory itself, so git descends and the file-level rules are evaluated normally. Unless you need the directory exclusion for volume or performance reasons, the second shape is easier to read.
Leading slashes and directory-only markers
A pattern without a slash matches a name at any depth, so build excludes src/build as well as the root build. Anchor it with a leading slash (/build) to limit it to the root. A trailing slash applies to directories only, so build/ leaves a file named build alone.
Orders that go wrong
| Intent | Broken list | Fixed list |
|---|---|---|
| Ignore build output but keep a sample | !dist/sample.jsondist/ | dist/*!dist/sample.json |
| Ignore all logs, keep the audit log | !audit.log*.log | *.log!audit.log |
| Ignore secrets, keep the example | .env*!.env.example.env | .env*!.env.example |
Files that are already tracked
.gitignore only affects untracked files. A file already committed keeps being tracked even after a rule appears. Remove it from the index with git rm --cached <path>, and list candidates with git ls-files -ci --exclude-standard, which prints the tracked files that a rule would ignore. That is the fastest way to find leftovers after a rule change.
Check it with a command
After editing, run git check-ignore -v <path>. The output names the file and the line number that decided, and no output means the path is not ignored. Add --no-index to test paths that do not exist yet.
Put the same rules and paths into the debugger to see the winning line and the traps immediately.
Open GlobLabFrequently asked questions
Why does a `!` rule at the top not work?
The last matching rule wins, so an earlier re-include is overridden by a later exclusion. Always put re-includes below the exclusion they are meant to reverse.
Is there no way to restore a file under an excluded directory?
Re-include the directory itself (!dir/) and adjust inside it, or exclude at the child level with dir/* from the start.
Which wins when a global and a repository .gitignore disagree?
Precedence is command line, then the repository .gitignore, then the repository core.excludesFile, then global settings. A higher-precedence source wins for the same target.
Sources
Read next
- Pattern syntax: .gitignore versus GitHub Actions pathsHow `**`, `*`, `?` and `[]` differ between the two systems, and why paths and paths-ignore cannot be combined.
- The rsync include/exclude first-match trapRule order in rsync, directory pruning, and a filter order that survives backups, with broken and fixed examples.
- .dockerignore versus .gitignoreTwo files with the same syntax and different scope, plus patterns that keep the build context small and reproducible.