THE GITHTML FIELD GUIDE
Write Git ignore patterns that match the intended files
A .gitignore file supplies patterns for intentionally untracked files. A leading slash anchors a pattern to that ignore file’s directory, a trailing slash targets directories, and an exclamation mark negates a pattern subject to parent-directory rules. Test actual paths with git check-ignore -v. Ignore rules do not stop tracking files already in the index.
Design rules around generated content
Ignore files that should not become part of repository history, such as local caches or reproducible build output. Do not use ignore rules to hide inconvenient source changes. A broad pattern like config can accidentally exclude a legitimate directory, while a narrowly anchored output path communicates the intended boundary. Treat the ignore file as shared project behavior that deserves review.
Patterns are interpreted relative to the location of their .gitignore file when they contain the relevant path separators. Nested ignore files can refine rules for subdirectories. A rule aimed at the repository root is therefore not interchangeable with the same text placed deeper in the tree. Test paths from more than one location when similar names occur throughout the project.
Check examples before committing the rules
The sample patterns ignore a root build directory, log files, and a cache directory. Add them in your editor only if they match the project’s real conventions. The command reports the ignore file, line, and pattern responsible for an ignored path, giving a concrete explanation rather than a guess based on status.
- To retain a file inside an otherwise ignored area, account for its parent directories. Git cannot re-include a file when its parent directory remains excluded from traversal.
- Check whether the path is already tracked before blaming pattern syntax. Tracked files continue to appear as changes regardless of a matching ignore rule.
# Example .gitignore entries
/build/
*.log
.cache/
# Inspect a real path
git check-ignore -v -- build/output.bin
git status --short --ignoredReview exclusions as part of project correctness
An overbroad ignore rule can make a developer’s checkout appear complete while a fresh clone lacks an essential file that was never committed. Test the project’s reproducibility and inspect newly created source files explicitly after adding ignore patterns. A quiet status display should be a consequence of correct tracking decisions, not the primary objective.
For secrets, an ignore rule is preventive only. If a credential was committed or shared, rotate it and follow the project’s incident process; adding its filename to .gitignore does not remove historical copies. Keep personal editor preferences in an appropriate local or global exclusion mechanism when they are not project policy. Finally, stage and review the ignore-file change itself so teammates can see why each new exclusion exists and challenge rules that would conceal important work.
A useful ignore rule names a class of disposable artifacts and has a clear owner who can explain why those files should never become project source.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.