Pattern syntax
| Pattern | Matches |
|---|---|
*.log |
Files ending in .log, in any directory |
/build |
build at the top of the repository only (a leading / anchors it) |
build/ |
Directories named build anywhere, but not a file called build |
docs/*.md |
.md files directly in docs/ (a / in the middle also anchors the pattern) |
docs/**/*.md |
.md files anywhere under docs/ |
**/temp |
temp anywhere, the same as just temp |
!keep.log |
Re-include keep.log after *.log excluded it |
\#file |
A file literally named #file (# would start a comment) |
Useful commands
git check-ignore -v dist/app.js # which rule ignores this file
git status --ignored # list ignored files too
git rm -r --cached node_modules # stop tracking files that are now ignored
git config --global core.excludesFile ~/.gitignore_global
git clean -ndX # preview deleting all ignored files (-n = dry run)
Keeping secrets out of the repository? Commit an .env.example and ignore .env; the
.env to JSON converter helps when you need the values elsewhere.
Frequently asked questions
Why is a file still tracked after I added it to .gitignore?
.gitignore only affects files git isn’t tracking yet. If the file was committed before, remove it from the index while keeping it on disk with git rm --cached path (git rm -r --cached for a directory), then commit. Don’t forget to rotate any secret that was committed: it’s still in the history.
Why doesn’t my ! rule re-include the file?
Because a parent directory is already ignored. Git doesn’t look inside ignored directories, so !cache/keep.txt does nothing after cache/. Ignore the contents instead of the directory: cache/* followed by !cache/keep.txt. The path checker shows when a file is ignored “inside” a directory for this reason.
Where do the templates come from?
From github/gitignore, the collection GitHub offers when you create a repository, released into the public domain (CC0). 39 of the most used templates are included. Rules that appear in more than one selected template are kept only once.
Should editor and OS files go in the project’s .gitignore?
Either works. Many teams add .DS_Store, .idea/ and .vscode/ to the project so nobody commits them by accident. The cleaner alternative is a global ignore file for your own machine: git config --global core.excludesFile ~/.gitignore_global, with your OS and editor rules in it.
How do I check why a file is ignored?
Run git check-ignore -v path/to/file. It prints the .gitignore file, line number and pattern that matched, the same information the path checker on this page shows. Add --no-index to check files that are already tracked.