A .gitignore file tells Git which files and folders to leave untracked: build output, dependency folders, editor settings, and secrets. Getting it right early keeps your repository small and clean and helps prevent accidents.
Pattern syntax
# Comments must be on their own line
# any .log file
*.log
# a directory named build, anywhere
build/
# only dist in the repo root
/dist
# temp directories at any depth
**/temp
# negate: track this file even though *.log is ignored
!important.log
# environment file with secrets
.env- * matches anything except a slash; ** matches directories at any depth.
- A trailing slash matches only directories.
- A leading slash anchors the pattern to the repo root.
- A # starts a comment only at the beginning of a line; text after a pattern on the same line becomes part of the pattern.
- Patterns are evaluated top to bottom, and later rules can override earlier ones.
- You can't re-include a file if a parent directory is ignored.
What to ignore
- Dependencies: node_modules/, vendor/, virtual environments.
- Build output: dist/, build/, .next/, target/, __pycache__/.
- Logs and temporary files: *.log, *.tmp.
- OS and editor files: .DS_Store, Thumbs.db, .idea/, .vscode/ (unless you share settings).
- Secrets and local config: .env, *.pem, credentials files.
It doesn't work on files already tracked
If a file was committed before you added it to .gitignore, Git keeps tracking it. Remove it from the index (not from disk) and commit:
git rm --cached path/to/file
git rm -r --cached node_modules
git commit -m "Stop tracking ignored files"Secrets: prevention and cleanup
Useful extras
- A global ignore file (git config --global core.excludesfile ~/.gitignore_global) handles personal editor and OS files across all repos.
- git check-ignore -v path shows which rule is ignoring a file.
- Start from a template for your language or framework, then trim it.
- Commit a .env.example with placeholder values so others know which variables are needed.
Frequently asked questions
+Why isn't my .gitignore working?
The file is probably already tracked. Run git rm --cached on it, then commit; the ignore rule will apply afterwards.
+How do I ignore a folder except one file?
Ignore the folder's contents with folder/* (not the folder itself), then add a negation like !folder/keep.txt.
+Should I commit .env files?
No. Commit a .env.example with dummy values and keep the real .env ignored.
+What is a global gitignore?
A personal ignore file that applies to all your repositories, useful for editor and OS files.
.gitignore Generator
Free, runs in your browser — nothing you enter is uploaded.