📁 .gitignore Cheatsheet
Complete Guide to Git Ignore Patterns & Best Practices
🎯 Basic Syntax
Ignore a file
filename.txtIgnores the specific file in any directory
Ignore a directory
folder/Ignores the entire directory and its contents
Wildcard match
*.logIgnores all files with .log extension
Negation (exception)
!important.logDon't ignore this file (even if matched before)
Comments
# This is a commentAdd comments to document your .gitignore
Root directory only
/config.jsonOnly ignores file in root, not subdirectories
🔧 Pattern Matching
| Pattern | Description | Example Matches |
|---|---|---|
| * | Matches any string (except /) | *.txt matches file.txt, data.txt |
| ** | Matches any number of directories | **/logs matches a/logs, a/b/logs |
| ? | Matches any single character | file?.txt matches file1.txt, fileA.txt |
| [abc] | Matches any character in brackets | file[123].txt matches file1.txt, file2.txt |
| [0-9] | Matches any character in range | file[0-9].txt matches file0.txt to file9.txt |
| ! | Negates a pattern (exception) | !important.log keeps this file tracked |
| / | Directory separator | /logs only ignores logs in root |
| # | Comment line | # Ignore all .env files |
💼 Common Use Cases
Operating System Files
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
Desktop.ini
$RECYCLE.BIN/
# Linux
*~
.directory
IDE & Editor Files
# Visual Studio Code
.vscode/
*.code-workspace
# JetBrains IDEs
.idea/
*.iml
*.iws
# Sublime Text
*.sublime-project
*.sublime-workspace
# Vim
*.swp
*.swo
*~
Language-Specific Patterns
# Node.js
node_modules/
npm-debug.log*
yarn-error.log*
.pnpm-debug.log*
# Python
__pycache__/
*.py[cod]
*$py.class
*.egg-info/
venv/
.env
# Java
*.class
*.jar
*.war
target/
# Ruby
*.gem
.bundle/
vendor/bundle/
# Go
*.exe
*.test
*.out
Build & Dependencies
# Build directories
dist/
build/
out/
bin/
# Dependency directories
node_modules/
vendor/
packages/
# Compiled files
*.o
*.so
*.dylib
*.dll
Environment & Secrets
# Environment variables
.env
.env.local
.env.*.local
# API keys and secrets
secrets.yml
*.pem
*.key
credentials.json
# Configuration with secrets
config/database.yml
config/secrets.yml
Logs & Temporary Files
# Log files
*.log
logs/
*.log.*
# Temporary files
tmp/
temp/
*.tmp
*.bak
*.swp
📋 Advanced Patterns
Ignore all except one
*.log
!important.logIgnores all .log files except important.log
Nested directory pattern
**/build/output/Matches build/output/ at any depth
Multiple extensions
*.{jpg,jpeg,png,gif}Ignores multiple image formats
Ignore directory but not root
**/temp/
!/temp/Ignores temp folders except in root
⚡ Best Practices
✓ DO:
- Add .gitignore before your first commit
- Use template .gitignore files for your language/framework
- Keep .gitignore in version control
- Document complex patterns with comments
- Use specific patterns over broad wildcards
- Group related patterns together
✗ DON'T:
- Commit sensitive information like API keys or passwords
- Ignore files that should be shared with team (configs, schemas)
- Use overly broad patterns that might catch important files
- Ignore the .gitignore file itself
- Forget to update .gitignore when adding new tools
🛠️ Useful Commands
# Check what's being ignored
git status --ignored
# Remove file from tracking (already committed)
git rm --cached filename
# Remove directory from tracking
git rm -r --cached directory/
# Test if a file would be ignored
git check-ignore -v filename
# Force add an ignored file
git add -f filename
# Apply .gitignore to already tracked files
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"
📦 Template Resources
Quick Start Templates:
- GitHub Templates: github.com/github/gitignore
- gitignore.io: Generate custom .gitignore files online
- toptal.com/developers/gitignore: Interactive generator
🔍 Troubleshooting
File still being tracked?
# If a file was committed before being ignored:
git rm --cached filename
git commit -m "Stop tracking filename"
Pattern not working?
- Check for typos in the pattern
- Ensure no trailing whitespace
- Use git check-ignore -v filename to debug
- Remember patterns are applied top-to-bottom
- Later patterns can override earlier ones with negation (!)
📝 Complete Example
# ===================================
# Project-Specific .gitignore Example
# ===================================
# Dependencies
node_modules/
vendor/
bower_components/
# Environment & Secrets
.env
.env.local
.env.*.local
secrets.yml
*.pem
# Build Output
dist/
build/
out/
*.min.js
*.min.css
# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Operating System
.DS_Store
Thumbs.db
*.swp
*~
# IDE
.vscode/
.idea/
*.sublime-workspace
# Testing
coverage/
.nyc_output/
*.test.js.snap
# Temporary Files
tmp/
temp/
*.tmp
*.bak
# Keep these exceptions
!.gitkeep
!.env.example
