The .gitignore File
Why Ignore Files?
Some files should never be committed to Git:
- Passwords & secrets (API keys, .env files)
- Dependencies (node_modules/, venv/)
- Build output (dist/, .next/, __pycache__/)
- OS files (.DS_Store, Thumbs.db)
- Editor files (.vscode/, .idea/)
Creating .gitignore
Create a file called .gitignore in your project root:
# Dependencies
node_modules/
venv/
__pycache__/Environment variables (SECRETS!)
.env
.env.localBuild output
dist/
build/
.next/OS files
.DS_Store
Thumbs.dbEditor files
.vscode/
.idea/
*.swp
Pattern Syntax
| Pattern | Meaning |
|---|---|
| <code>file.txt</code> | Ignore this specific file |
| <code>.log</code> | Ignore all .log files |
| <code>folder/</code> | Ignore entire folder |
| <code>!important.log</code> | Don't ignore this file (exception) |
| <code>*/temp</code> | Ignore "temp" in any directory |
Common Templates
GitHub provides templates for many languages:
| Language/Framework | Key Ignores |
|---|---|
| Node.js | node_modules/, .env |
| Python | venv/, __pycache__/, .pyc |
| Java | target/, .class |
| React/Next.js | .next/, build/, node_modules/ |
💡 Tip: Visit github.com/github/gitignore for ready-made templates.
Already Tracked Files
If a file was committed before adding it to .gitignore, you need to untrack it:
# Remove from Git tracking (keeps the file locally)
git rm --cached .env
git commit -m "Stop tracking .env"
Summary
| Concept | Purpose |
|---|---|
| <code>.gitignore</code> | List files Git should ignore |
| <code>*.ext</code> | Wildcard patterns |
| <code>folder/</code> | Ignore directories |
| <code>git rm --cached</code> | Stop tracking a committed file |