Sub topic 5
Re-including Files with !
This chapter assumes you can run git init, git add, git commit, and git status, and that you know the basics of .gitignore patterns such as *, directory patterns ending in /, and anchoring with a leading /. Here you will learn how to make exceptions to an ignore rule, and why those exceptions sometimes have no effect.
What negation does
A line that starts with ! is a negation pattern. It re-includes files that an earlier pattern ignored. Git reads the rules in order, and for a given path the last matching pattern wins.
*.log
!important.log
logs/*
!logs/keep.txtThe first pair ignores every file ending in .log, then makes an exception for important.log. The second pair ignores everything directly inside logs/, then makes an exception for logs/keep.txt.
Order matters. If you put the negation first, the later *.log rule matches important.log again and wins, so the exception does nothing.
# Wrong order: *.log is the last match, so important.log stays ignored
!important.log
*.logThe parent directory limitation
Git does not look inside a directory that is ignored. If a rule excludes the directory itself, a negation for a file inside it is never evaluated.
# Does not work: Git never looks inside an ignored directory
logs/
!logs/keep.txtThe pattern logs/ ignores the directory as a whole. Git skips it and never reaches logs/keep.txt, so the ! line has no effect.
The working version in the first example uses logs/*. That pattern ignores the contents of logs/ one entry at a time and leaves the directory itself unignored. Git still looks inside it, and the negation can match keep.txt.
Re-including inside nested directories
If you want to keep one file deep in an ignored tree, you must re-include each directory on the path, then ignore the other contents at each level.
build/*
!build/reports/
build/reports/*
!build/reports/summary.txtHere build/* ignores everything in build/. The line !build/reports/ makes reports visible again. Then build/reports/* ignores its contents, and the last line keeps summary.txt.
Checking the result
After you change the rules, verify them with git check-ignore -v. It prints the file, line number, and pattern that decide the outcome.
git check-ignore -v logs/keep.txt
git check-ignore -v logs/debug.txtFor logs/keep.txt with the working rules, the output shows the negation line (for example .gitignore:4:!logs/keep.txt). For logs/debug.txt, it shows logs/*. If a path is not ignored and no rule matches, the command prints nothing.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Negated file is still ignored | The parent directory is ignored with `dir/` | Use `dir/*` instead |
| Negation has no effect | It appears before the ignore rule | Move the `!` line after the rule |
| Deep file is still ignored | An intermediate directory was not re-included | Add a `!` line for each directory on the path |
| Pattern with `!` is treated as a literal name | You meant a name that starts with `!` | Escape it as `\!name` |
Summary
!patternre-includes a path that an earlier pattern ignored.- The last matching pattern wins, so place negations after the rules they override.
- Git never looks inside an ignored directory, so
dir/followed by!dir/filefails. - Use
dir/*to ignore contents while keeping the directory visible to Git. - Confirm the outcome with
git check-ignore -v.
Exercise
Your .gitignore contains config/ followed by !config/defaults.json, yet git status never shows config/defaults.json. Why, and how do you fix it?
Show answer
The pattern config/ ignores the whole directory, so Git never looks inside it and the negation is never evaluated. Change the first line to config/* so only the contents are ignored. Keep !config/defaults.json after it. Verify with git check-ignore -v config/defaults.json, which should report the negation line.
Exercise
Write rules that ignore everything in assets/ except assets/logo.png and the whole assets/icons/ directory.
Show answer
Use these lines:
assets/*
!assets/logo.png
!assets/icons/
The first line ignores every entry in assets/ while leaving the directory visible. The next two lines re-include the logo file and the icons directory, which is allowed because assets/ itself is not ignored.