.gitignore Cheatsheet - Ignore Patterns & Rules
Committing node_modules or .env by accident is a trap nearly every repo hits once. This .gitignore reference organizes 5 groups — basic matching syntax, wildcards and comments, directories vs files, global ignore and effect commands, plus common language templates. When unsure whether a rule matches, verify with git check-ignore -v; for already-tracked files, un-track with git rm --cached first. After reading you can write ignore rules that stay safe without leaking files.
Basic Syntax 6
*.logbuild//dist**/node_modulesdoc/*.txt!keep.txtWildcards & Comments 6
?[abc]**# note\#file\!impDirs & Files 4
.cache*.tmplogs/*.log*.swpGlobal & Effects 4
git config --global core.excludesfile ~/.gitignore_globalgit check-ignore -v filegit rm --cached filegit add -f fileCommon Language Templates 4
node_modules/.env__pycache__/.DS_StoreTips
- 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.
FAQ
How do I write a .gitignore?
Create .gitignore at the repo root with one pattern per line: add a trailing slash for directories (build/), write the file name directly (.env), use * as a wildcard (*.log), and a leading ! to un-ignore. Adding common dependency and secret patterns like node_modules/ and .env prevents accidental commits.
How do I ignore node_modules and .env in Git?
Add two lines to .gitignore: node_modules/ and .env (cover variants like .env.local with .env* plus !.env.example). For already-tracked files, run git rm --cached first so the ignore actually takes effect.
What is the difference between .gitignore and a global gitignore?
A project .gitignore applies only to that repo; a global ignore (git config --global core.excludesfile ~/.gitignore_global) applies to every repo, good for editor caches and OS dotfiles. The syntax is the same.
How do I check whether a .gitignore rule matches a file?
Run git check-ignore -v <file> to see which line and which file matched; no output means the file is not ignored. When debugging, check the path syntax and the order of negation rules.
What do the exclamation mark and slash mean in .gitignore?
A trailing / matches directories only; a leading ! negates a rule and re-includes an otherwise ignored item (the negation must come after the broader ignore); a leading / anchors the pattern to the repo root.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Aug 2, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us