Git By Example
.gitignore Patterns
1 min read
Appendix B — .gitignore Patterns
How .gitignore Works
Each line in .gitignore is a pattern. Git ignores matching files.
# This is a comment
*.log # Ignore all .log files
!important.log # But NOT this one
/build # Ignore build/ in root only
build/ # Ignore any directory named build
**/temp # Ignore temp anywhere in the tree
doc/**/*.pdf # Ignore PDFs in doc/ subdirectories
Pattern Syntax
Pattern Matches
───────────────────────────────────────────
*.ext Any file with that extension
dir/ A directory (and its contents)
/dir Only in the repo root
**/pattern At any depth
pattern/** Everything inside pattern/
!pattern Negate (un-ignore) a previous rule
? Any single character
[abc] Any character in the set
[0-9] Any digit
Common .gitignore Templates
Node.js
node_modules/
dist/
build/
.env
.env.local
*.log
npm-debug.log*
.DS_Store
coverage/
Python
__pycache__/
*.py[cod]
*.egg-info/
dist/
build/
.venv/
venv/
.env
*.egg
.pytest_cache/
.mypy_cache/
Java
*.class
*.jar
*.war
target/
.gradle/
build/
.idea/
*.iml
Go
bin/
vendor/
*.exe
*.test
*.out
Rust
target/
Cargo.lock # only for libraries, keep for binaries
General / IDE
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
desktop.ini
# Linux
*~
.nfs*
# IDEs
.idea/
.vscode/
*.swp
*.swo
*~
.project
.classpath
.settings/
# Environment
.env
.env.local
.env.*.local
Global .gitignore
Set a global ignore file for patterns that apply to all your repos:
git config --global core.excludesfile ~/.gitignore_global
# ~/.gitignore_global
.DS_Store
Thumbs.db
*.swp
.idea/
.vscode/
Ignoring Already-Tracked Files
.gitignore only affects untracked files. To ignore a file that's already tracked:
git rm --cached filename
echo "filename" >> .gitignore
git commit -m "Stop tracking filename"
Debugging .gitignore
Example: Check why a file is ignored
git check-ignore -v path/to/file
.gitignore:3:*.log debug.log
This tells you which rule in which file is causing the ignore.
Resources
The github/gitignore repository has comprehensive .gitignore templates for virtually every language and framework.