Git Ignore — The .gitignore File Explained
In this tutorial, you'll learn about Git Ignore. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
Master .gitignore — create effective ignore rules, use patterns, set up global ignores, and force-track files that are already ignored.
Why It Matters
Without .gitignore, build artifacts, dependencies, and secrets get committed. A good .gitignore keeps your repo clean and secure.
Real-World Use
Preventing node_modules/, .env, *.pyc, and build folders from ever being committed.
What is .gitignore?
A .gitignore file tells Git which files to ignore — they won't be tracked, staged, or shown in git status.
# Don't track these:
node_modules/
.env
*.log
dist/
Pattern Reference
# Comments start with #
# Specific file
password.txt
# Directory
node_modules/
build/
dist/
# Wildcard (all .log files)
*.log
# Wildcard (all .tmp files in any directory)
**/*.tmp
# Ignore everything in a directory except
data/
!data/important.csv
# Ignore files in root only (not subdirectories)
/secret.txt # Only ./secret.txt, not ./src/secret.txt
# Negate pattern (don't ignore)
*.md
!README.md # Track README.md even though all .md ignored
# Empty directories
# Git doesn't track empty dirs, so no rule needed
# Pattern matching
logs/ # Ignore logs/ directory at any level
/logs/ # Only logs/ in the root
*.log # All .log files
**/logs/ # All logs/ directories recursively
# Character range
*.[Oo]bj # *.obj and *.Obj
# Ignore files in any directory with a name
.DS_Store # macOS
Thumbs.db # Windows
Templates for Common Languages
Node.js
node_modules/
npm-debug.log*
.env
.env.*
*.tsbuildinfo
dist/
.next/
.cache/
Python
__pycache__/
*.py[cod]
*.so
*.egg-info/
dist/
build/
.venv/
venv/
env/
.pytest_cache/
.mypy_cache/
.ruff_cache/
Java
*.class
*.jar
*.war
target/
build/
.gradle/
.idea/
*.iml
.settings/
.project
Go
*.exe
*.exe~
*.dll
*.so
*.dylib
vendor/
React / Vite
node_modules/
dist/
.env.local
*.local
Tracked Files Ignored
If a file is already tracked, .gitignore won't stop it. You must untrack it:
# Remove from tracking (but keep on disk)
git rm --cached .env
git commit -m "Stop tracking .env"
# Now add to .gitignore
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Add .env to gitignore"
Global Gitignore
Set up global rules for your entire system:
# Create a global gitignore
git config --global core.excludesFile ~/.gitignore_global
# Edit ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_global
echo "Thumbs.db" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_global
Good for OS-specific files, editor swap files, etc.
Per-Directory Gitignore
Git respects .gitignore files in subdirectories too:
my-project/
├── .gitignore # Project-wide rules
├── api/
│ └── .gitignore # API-specific rules
├── frontend/
│ └── .gitignore # Frontend-specific rules
└── docs/
└── .gitignore # Docs-specific rules
Checking Why a File is Ignored
# Is it ignored?
git check-ignore -v config.yml
# .gitignore:3:*.yml config.yml
# What pattern is causing it?
git check-ignore node_modules/some-file.js
# .gitignore:1:node_modules/ node_modules/some-file.js
Force Adding Ignored Files
# Override .gitignore for specific files
git add -f config.production.yml
# Check if it's now tracked
git status
.gitignore Best Practices
| Rule | Why |
|---|---|
| Ignore dependencies, not lock files | yarn.lock should be tracked |
Commit .gitignore early |
Avoid accidental commits |
| Use global gitignore for OS files | Every project shouldn't repeat .DS_Store |
| Use descriptive comments | Explain "why" for unusual ignores |
Test patterns with git check-ignore |
Confirm expected behavior |
| Never commit secrets | Even if ignored, check for past commits |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro