.gitignore is a file that tells Git which files NOT to track.
Why Do You Need It?
Without .gitignore you might accidentally commit:
- node_modules/ — 200 MB of dependencies
- .env — database passwords and API keys
- *.log — log files taking up tens of MB
- .DS_Store — macOS junk files
What NOT to Commit
# Dependencies (installed via pip/npm)
__pycache__/
*.pyc
.venv/
venv/
node_modules/
# Secrets and passwords — NEVER!
.env
.env.local
secrets.yml
*.pem
*.key
# Compiled code
dist/
build/
*.exe
*.pyc
# Logs and temp files
*.log
*.tmp
# OS system files
.DS_Store # macOS
Thumbs.db # Windows
# IDE configs
.vscode/
.idea/
# Local databases
*.sqlite
*.sqlite3
db.sqlite3
Syntax
secret.txt # specific file
logs/ # directory
*.log # all files with extension
**/temp.txt # in any subdirectory
!important.log # exception — do NOT ignore
# comment
Templates by Language
Python / Django
__pycache__/
*.py[cod]
.venv/
venv/
dist/
build/
*.egg-info/
.env
db.sqlite3
media/
staticfiles/
Node.js
node_modules/
dist/
build/
.next/
npm-debug.log*
.env
Java
*.class
*.jar
target/
build/
.gradle/
Ready-made templates: gitignore.io or select a template when creating a repository on GitHub.
Management
Global .gitignore (applies to all projects):
git config --global core.excludesfile ~/.gitignore_global
Check whether a file is ignored:
git check-ignore -v node_modules/
git status --ignored
If You Accidentally Committed a Secret
- Immediately rotate the compromised key/password
- Remove from history:
bash git filter-branch --tree-filter 'rm -f .env' HEAD git push --force - Note: old commits may still exist in forks
Common Mistakes
node_modules # ❌ — ignores a file named "node_modules"
node_modules/ # ✅ — ignores the directory
.env # ❌ — leading space, won't work
.env # ✅
Create .gitignore at the very start of the project — removing a file from Git history is much harder than never adding it.
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!