.gitignore
What is .gitignore?
A .gitignore file tells Git which files and folders to ignore — meaning they won't be tracked, staged, or committed. You place it in the root of your repository.
Files you almost always want to ignore:
- Dependencies —
node_modules/,venv/,.venv/ - Environment files —
.env,.env.local(contain secrets!) - Build output —
dist/,build/,.next/ - IDE/editor settings —
.idea/,.vscode/(optional, personal preference) - OS files —
.DS_Store(macOS),Thumbs.db(Windows) - Logs —
*.log,npm-debug.log
Creating a .gitignore
Create a file named exactly .gitignore (note the leading dot) in your project root:
touch .gitignore
Then add patterns, one per line:
# Dependencies
node_modules/
# Environment variables — NEVER commit these
.env
.env.local
.env.*.local
# Build output
dist/
build/
.next/
out/
# Logs
logs/
*.log
npm-debug.log*
# OS files
.DS_Store
Thumbs.db
# IDE settings
.idea/
.vscode/
*.swp
*.swo
Pattern Syntax
| Pattern | What it ignores |
|---|---|
node_modules/ | The folder named node_modules anywhere |
*.log | Any file ending in .log |
.env | A file named exactly .env |
*.env | Any file ending in .env |
dist/ | The dist folder |
!dist/index.js | But not this specific file (negate with !) |
**/logs | logs folder anywhere in the tree |
debug.log | Only at root level |
/debug.log | Same — / anchors to root |
Ready-Made Templates
GitHub provides official .gitignore templates for every language and framework. When creating a new repo on GitHub, you can select a template from the dropdown.
Or use gitignore.io — type in your tech stack (e.g., Node,React,macOS) and it generates a complete .gitignore for you.
Common starting points:
Node.js / Express:
node_modules/
.env
.env.*
dist/
*.log
.DS_Store
React (Create React App):
node_modules/
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
build/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_Store
React (Vite):
node_modules/
dist/
.env
.env.local
*.local
.DS_Store
Ignoring Already-Tracked Files
.gitignore only prevents untracked files from being tracked. If you accidentally committed a file (like .env) and later added it to .gitignore, Git still tracks it.
To stop tracking it without deleting it locally:
git rm --cached .env
git commit -m "Remove .env from tracking"
Now .env stays in your working directory but is no longer in Git history going forward.
If you've committed API keys, passwords, or tokens to a public repository, treat them as compromised immediately:
- Revoke and rotate the exposed credentials
- Use
git filter-branchor BFG Repo Cleaner to purge them from history - Force-push the cleaned history
Removing from history is complex — it's far easier to never commit secrets in the first place.
A Practical Tip: Global .gitignore
You can set a global .gitignore for files you always want to ignore regardless of project — like OS and editor files. This way you don't add .DS_Store to every project's .gitignore:
git config --global core.excludesfile ~/.gitignore_global
Then add your personal ignores to ~/.gitignore_global:
# macOS
.DS_Store
.AppleDouble
# Windows
Thumbs.db
Desktop.ini
# Editor
.vscode/
.idea/
*.swp