.gitignore Cheatsheet - Ignore Patterns & Rules

The glob syntax and pitfalls you forget when writing .gitignore, grouped by wildcards, directories, negation and global config, with copy-ready examples.

Version Control·24 commands·Last updated 2026-08-02
Back to Version Control

Basic Syntax 6

*.log
Ignore all .log files
build/
Ignore the build directory and its contents
/dist
Ignore dist only at repo root (not subdirs)
**/node_modules
Ignore node_modules at any depth
doc/*.txt
Ignore .txt one level under doc (not nested)
!keep.txt
Negation: do NOT ignore keep.txt

Wildcards & Comments 6

?
Match a single character
[abc]
Match any char in brackets
**
Match any number of directories
# note
Comment line
#file
Escape # to match a literal hash
!imp
Escape ! to match a literal bang

Dirs & Files 4

.cache
Ignore a file or dir named .cache
*.tmp
Ignore all temp files
logs/*.log
Ignore logs one level deep
*.swp
Ignore editor swap files

Global & Effects 4

git config --global core.excludesfile ~/.gitignore_global
Set a global ignore file
git check-ignore -v file
Show which rule ignores a file
git rm --cached file
Stop tracking but keep the local file
git add -f file
Force-add an ignored file

Common Language Templates 4

node_modules/
Node dependencies dir
.env
Env vars (secrets, always ignore)
__pycache__/
Python cache
.DS_Store
macOS system file

💡 Tips

  • Patterns are relative to the .gitignore location; use /dist to anchor at root.
  • A negation !file only works if its parent directory is not ignored.
  • Use git check-ignore -v file to see which rule ignores a file.
  • For already-tracked files, git rm --cached stops tracking but keeps the file.

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Aug 2, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it