Gitignore Cheat Sheet

Master .gitignore — pattern syntax, global gitignore, negation, directory-specific rules, and best practices for keeping repositories clean.

Last Updated: May 1, 2025

Pattern Syntax

PatternMatchesExample
*.logAll .log fileserror.log, debug.log
build/Any directory named buildbuild/, src/build/
**/temptemp anywhere in treetemp, src/temp, a/b/temp
*.jsAll .js in this directoryapp.js (not subdir/file.js)
**/*.jsAll .js at any depthapp.js, lib/util.js
!important.logNegation: DON'T ignoreOverrides *.log for this file
/TODOOnly TODO at root/TODO (not src/TODO)
doc/**/*.txtAll .txt in doc/ treedoc/readme.txt, doc/ref/manual.txt

Essential .gitignore Entries

node_modules/
Ignore Node.js dependencies (always)
dist/
Ignore build output directory
.env
Ignore environment files with secrets
*.pyc
Ignore compiled Python bytecode
__pycache__/
Ignore Python cache directory
.DS_Store
Ignore macOS directory metadata files
*.swp
Ignore Vim swap files
coverage/
Ignore test coverage reports

Global Gitignore

git config --global core.excludesfile ~/.gitignore_global
Set up a global gitignore file
nano ~/.gitignore_global
Create and edit your global gitignore
echo '.DS_Store' >> ~/.gitignore_global
Add macOS metadata files globally
git config --global core.excludesfile
Check if global gitignore is configured
git check-ignore -v file
Find which gitignore rule is ignoring a file
git rm --cached file
Stop tracking a file (add it to gitignore first)

Best Practices

ItemDescription
Commit EarlyAdd .gitignore as your first commit to prevent tracking unwanted files
Environment FilesAlways ignore .env — use .env.example with placeholder values instead
Generated FilesIgnore anything produced by builds, compilers, or package managers
OS FilesUse global gitignore for OS-specific files (.DS_Store, Thumbs.db)
Local OverridesUse .git/info/exclude for personal ignores you don't want to share
Check Before Addgit status before git add . — catch files you forgot to gitignore
Team ConsistencyReview .gitignore in PRs to ensure team agreement
Un-ignore Tracked Filesgit 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.