Gitignore Patterns: Ignore Generated Files
Create a shared ignore policy
A .gitignore file prevents ordinary staging of matching untracked files. In the ignore fixture, the root .gitignore already contains these rules. *.log matches names ending in .log at any depth below this file; /scratch.txt applies at this file’s directory; build/ matches directories named build; !keep.log reverses the earlier log rule for accessible paths.
In the fixture, debug.log, docs/trace.log, root scratch.txt and build/out.txt are ignored. keep.log and docs/scratch.txt are not. * does not match slash; ** has special recursive forms such as docs/**/draft.txt. A trailing slash selects directories, not a regular file of that name.
Diagnose matches and precedence
Run the following from the fixture root. A verbose check identifies the file, line and pattern associated with a match; a matching negation means inclusion, not exclusion. A plain check of a single untracked path returns status 0 if ignored and 1 if not.
git check-ignore -v -- debug.loggit check-ignore -v -- keep.loggit status --short --ignoredThe first reports the root log pattern and the second reports !keep.log. Ignore precedence is command-line patterns where supported, then per-directory .gitignore (deeper applicable files override higher ones), then .git/info/exclude, then core.excludesFile. Within a precedence level, later matching rules win. Use .gitignore for shared rules, .git/info/exclude for one repository’s local rules and core.excludesFile for personal rules.
Keep one file inside an otherwise ignored directory
In a separate exceptions fixture the root ignore rules are shown below. Ignoring build/ and then negating !build/keep.txt cannot reopen its excluded parent. Instead exclude the contents while leaving the parent traversable.
build/out.txt is ignored and build/keep.txt is eligible for staging. Commit the ignore policy itself when sharing it. Practice: predict what changes if a nested directory adds a conflicting rule. Ignore rules are not secret protection, access control or a way to remove already recorded history.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gitignore Patterns: Ignore Generated Files — knowledge check
Apply the file management and ignore rules taught in this lesson.
In a root .gitignore, which pattern applies only to root scratch.txt?
Given *.log then !keep.log, which file is eligible for staging in the root?
Why does !build/keep.txt fail after build/ excludes the entire directory?
Which command identifies the matching ignore rule?
Where should a shared generated-file rule usually live?