.gitignore Syntax Cheat Sheet - 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_StoreCommand Examples
Find which rule is ignoring a file
git check-ignore -v .envThe two fields of output name the rule file that matched and the rule text itself, which makes this the direct way to debug a pattern that looks right but never fires. No output at all means the file is not ignored.
Output
.gitignore:18:.env .env
The file is already tracked, so gitignore alone does nothing
git rm --cached .env
git commit -m "stop tracking .env".gitignore only affects untracked files. A file already in the index has to leave it first with git rm --cached, which keeps the working copy and the history intact. A leaked secret also needs rotating: editing the ignore file changes nothing about what was already published.
Output
rm '.env'
Ignore dist only at the root, keeping same-named subdirectories
/distThe leading slash anchors the pattern to the repository root, while dist/ or **/dist matches at any depth. In a monorepo that anchoring difference decides whether you exclude the real build output or also hit identically named subpackages.
Common Pitfalls
- Adding .env to .gitignore does not retract a key that was already committed — rotate the key; ignoring only prevents future commits.
- Ignoring rules have no effect on already-tracked files; use git rm --cached to remove them from the index.
- A leading slash anchors a pattern to the repository root only. Dropping it matches every level and can wrongly ignore monorepo subpackages.
- Do not use force-adding as a habit: git add -f pulls build artifacts and secrets back into the repository.
- A negation like !keep.txt only works if it comes after the rule it overrides; ordering matters.
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.
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