Configuring .gitignore
How .gitignore works: glob patterns, ignoring node_modules and .env secrets, untracking previously committed files, and global gitignore.
Configuring .gitignore
A .gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
1. Standard Production .gitignore Template (Node/Next.js)
# Dependencies
node_modules/
.pnpm-store/
# Local Environment & Secrets
.env
.env*.local
*.pem
*.key
# Build outputs
.next/
dist/
out/
build/
# OS and Editor metadata
.DS_Store
Thumbs.db
.vscode/*
!.vscode/settings.json
!.vscode/launch.json2. .gitignore Pattern Syntax
| Pattern | Behavior | Example |
|---|---|---|
filename | Ignores file anywhere in repository | secret.key |
/dir/ | Ignores directory relative to location of .gitignore | /dist/ |
*.log | Glob wildcard matching all .log files | debug.log, app.log |
!important.log | Negation: Tracks important.log even if *.log was ignored | !important.log |
logs/**/err.txt | Recursively matches nested directories | logs/2026/09/err.txt |
3. Untracking Files Already Committed to Git
If a sensitive file or node_modules/ was committed to Git before you added it to .gitignore, Git will continue tracking it until you remove it from the index:
# Remove from Git index while preserving file on local disk:
git rm --cached sensitive-config.json
# Or untrack an entire directory:
git rm -r --cached node_modules/
# Commit the removal:
git commit -m "chore: remove tracked node_modules from Git index"Related Topics
git reflog — The Developer Safety Net
How to rescue lost commits and branches with git reflog: reference logs, recovering from accidental git reset --hard, and recovering deleted branches.
Resolving Git Merge Conflicts
Step-by-step guide to resolving merge conflicts: conflict markers (<<<<<<<, =======, >>>>>>>), 3-way merge tools, and preventing conflicts.