.gitignore Syntax & Pattern Matching Guide
Comprehensive technical breakdown of pattern formatting, wildcard globbing, leading and trailing slashes, negation nuances, and character ranges in Git ignore files.
Every line in a .gitignore file represents an independent pattern, with three core formatting rules:
- Blank lines are ignored and serve as visual separators to keep files readable.
- Lines beginning with # are comments and have no effect on file tracking.
- Trailing spaces are ignored unless escaped with a backslash (\ ).
# Ignore compiled Python bytecode
__pycache__/
*.py[cod]
# Ignore local secret configurations
.env
.env.localGit supports standard shell globbing wildcards within a single directory level:
- * (Asterisk): Matches zero or more characters in a single path segment. *.log matches error.log, but not nested/error.log if anchored.
- ? (Question Mark): Matches exactly one single character. test?.js matches test1.js, but not test12.js.
- [abc] (Range): Matches any single character within the bracket set. *.[oa] matches object files ending in .o or archive files in .a.
The placement of forward slashes (/) completely alters how Git interprets patterns:
- Trailing Slash (logs/): Forces the pattern to match only directories. It will ignore a directory named logs/, but will not ignore a regular file named logs.
- Leading Slash (/config.json): Anchors the match to the root level where .gitignore resides. It ignores /config.json, but allows /packages/app/config.json to remain tracked.
- Middle Slash (docs/*.html): A slash anywhere in the middle also anchors relative to the directory containing the .gitignore file.
The double asterisk matches across multiple directory hierarchies:
- **/cache: Matches any folder or file named cache anywhere in the repository (cache, src/cache, a/b/c/cache).
- logs/**: Matches all files and subdirectories inside the logs/ directory, no matter how deeply nested.
- a/**/b: Matches a/b, a/x/b, a/x/y/b, and so forth.
An exclamation mark (!) negates a rule, telling Git to track a file even if an earlier pattern would have ignored it.
For performance reasons, Git stops traversing directories as soon as a directory is ignored. If a parent directory is excluded with build/, Git will NEVER re-include a file inside it using !build/important.txt!
To properly un-ignore a file inside an ignored folder, you must un-ignore the folder structure while ignoring its contents:
# INCORRECT: Will NOT work because parent directory is skipped
node_modules/
!node_modules/my-local-package
# CORRECT: Ignore contents of directory, then whitelist the specific target
node_modules/*
!node_modules/my-local-package/When you aren't sure why a file is being ignored (or why it isn't), use Git's built-in diagnosis command with the verbose (-v) flag:
# Shows the exact .gitignore file and line number responsible
git check-ignore -v src/temp/debug.logFrequently Asked Questions
Practical answers to common developer questions about .gitignore rules, caching, and repository configuration.
What is the difference between * and ** in .gitignore?
A single asterisk (*) matches characters within a single folder level. A double asterisk (**) matches across arbitrary nested directory hierarchies.
Why doesn't !folder/file.txt work when folder/ is ignored?
Git stops traversing any directory once it is ignored. To whitelist a file inside an ignored folder, ignore the folder contents with 'folder/*' and whitelist the target.
How do I see which rule is ignoring a file?
Run 'git check-ignore -v <filepath>'. Git will print the exact .gitignore file and line number matching that path.
.gitignore Guides & Reference
How to Ignore Already Tracked Files in Git
The definitive guide to resolving the common Git issue: untracking sensitive or generated files that were accidentally committed before adding a .gitignore.
How to Ignore a Directory in Git
Clear explanation of directory ignore rules: trailing slash behavior, root-relative folders, subdirectories, and keeping empty directories.
.gitignore Examples for Modern Tech Stacks
Curated, practical .gitignore examples for popular languages, backend frameworks, game engines, mobile development, and containerized stacks.