.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.

Version Control·24 commands·Last updated 2026-08-02
gitignoregitIgnorevcs

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.

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