.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_StoreCommand Examples
Find which rule is ignoring a file
git check-ignore -v .env输出两列分别是命中的规则文件与规则内容,排「我明明加了忽略规则却不生效」的问题时最直接。没有任何输出说明文件未被忽略。
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 只影响未追踪的文件。已入库的文件必须先 git rm --cached 移出索引,本地文件与提交历史都保留。密钥文件还需轮换,已泄露的密钥改 ignore 无效。
Output
rm '.env'
Ignore dist only at the root, keeping same-named subdirectories
/dist开头的斜杠锚定到仓库根;而 dist/ 或 **/dist 会匹配任意层级。路径锚定这个区别在 monorepo 里很关键,否则会误伤子包。
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