Git Ignore Patterns Explained
Git Ignore Patterns Explained
A well-crafted .gitignore keeps your repository clean by excluding build artifacts, dependencies, secrets, and OS-specific files. Understanding the pattern syntax helps you write precise rules.
Pattern Syntax
Basic Patterns
Ignore a specific file
secret.env
Ignore all files with an extension
*.log
Ignore a directory
node_modules/
Wildcards
* — matches anything except /
** — matches anything including /
? — matches any single character
[abc] — matches a, b, or c
[0-9] — matches digits
Negation
Ignore all .env files
*.env
But keep .env.example
!.env.example
Directory vs File
Trailing slash = directory only
build/
No trailing slash = file or directory
build
Anchored Patterns
Leading slash = relative to .gitignore location
/dist # Only root dist, not src/dist
No leading slash = matches anywhere
dist # Matches /dist AND /src/dist
Essential Patterns by Language
Node.js
node_modules/
.next/
dist/
*.tsbuildinfo
.env*.local
Python
__pycache__/
*.pyc
.venv/
dist/
*.egg-info/
General
OS files
.DS_Store
Thumbs.db
Editor files
.vscode/settings.json
.idea/
*.swp
Environment
.env
.env.local
Common Mistakes
Ignoring already-tracked files — .gitignore only affects untracked files. To stop tracking a file:git rm --cached filename
Not ignoring .env — secrets in git history are permanent. If you accidentally committed a secret, rotate it immediately.
Ignoring too much — don't ignore lock files (package-lock.json, yarn.lock). They ensure reproducible builds.
Global Gitignore
Set up a global gitignore for OS/editor files so you don't repeat them in every repo:
git config --global core.excludesFile ~/.gitignore_global
Try It
Use our .gitignore Generator to select your tech stack and generate a comprehensive .gitignore file instantly.
Conclusion
A good .gitignore is part of project hygiene. Understand the pattern syntax, set up a global gitignore for personal files, and never commit secrets.