Last Updated: May 1, 2025
Pattern Syntax
| Pattern | Matches | Example |
|---|---|---|
| *.log | All .log files | error.log, debug.log |
| build/ | Any directory named build | build/, src/build/ |
| **/temp | temp anywhere in tree | temp, src/temp, a/b/temp |
| *.js | All .js in this directory | app.js (not subdir/file.js) |
| **/*.js | All .js at any depth | app.js, lib/util.js |
| !important.log | Negation: DON'T ignore | Overrides *.log for this file |
| /TODO | Only TODO at root | /TODO (not src/TODO) |
| doc/**/*.txt | All .txt in doc/ tree | doc/readme.txt, doc/ref/manual.txt |
Essential .gitignore Entries
node_modules/Ignore Node.js dependencies (always)
dist/Ignore build output directory
.envIgnore environment files with secrets
*.pycIgnore compiled Python bytecode
__pycache__/Ignore Python cache directory
.DS_StoreIgnore macOS directory metadata files
*.swpIgnore Vim swap files
coverage/Ignore test coverage reports
Global Gitignore
git config --global core.excludesfile ~/.gitignore_globalSet up a global gitignore file
nano ~/.gitignore_globalCreate and edit your global gitignore
echo '.DS_Store' >> ~/.gitignore_globalAdd macOS metadata files globally
git config --global core.excludesfileCheck if global gitignore is configured
git check-ignore -v fileFind which gitignore rule is ignoring a file
git rm --cached fileStop tracking a file (add it to gitignore first)
Best Practices
| Item | Description |
|---|---|
Commit Early | Add .gitignore as your first commit to prevent tracking unwanted files |
Environment Files | Always ignore .env — use .env.example with placeholder values instead |
Generated Files | Ignore anything produced by builds, compilers, or package managers |
OS Files | Use global gitignore for OS-specific files (.DS_Store, Thumbs.db) |
Local Overrides | Use .git/info/exclude for personal ignores you don't want to share |
Check Before Add | git status before git add . — catch files you forgot to gitignore |
Team Consistency | Review .gitignore in PRs to ensure team agreement |
Un-ignore Tracked Files | git rm --cached && commit after updating .gitignore |
Pro Tip: Commit your .gitignore early — before adding any files. Once a file is tracked, adding it to .gitignore won't stop tracking it.