Reading the patterns
*.log any file ending in .log, at any depth
build/ a folder called build, at any depth
/config.php only config.php in the repository root
docs/**/*.pdf PDFs anywhere under docs
!keep.log an exception to an earlier rule
# note a comment
A leading slash anchors a pattern to the folder holding the .gitignore. A slash in the middle anchors it too, so src/temp matches only that exact path, while temp alone matches at any depth.
Rule order and exceptions
Git reads the file from top to bottom, and the last matching rule wins. That is why !.env.example must come after .env.*, and the Node template here writes them in that order.
An exception cannot rescue a file whose parent folder is excluded, since Git does not look inside an ignored folder at all. To keep one file in logs/, write logs/* in place of logs/, then add !logs/keep.txt.
Lock files should be committed
Files such as package-lock.json, yarn.lock, composer.lock and Cargo.lock record the exact dependency versions that were tested. Committing them means your teammates and your deployment install the same code you ran. The templates here leave them out for that reason.
The VS Code template shows a middle path for editor settings. It ignores the folder’s contents but keeps settings.json, extensions.json and launch.json so a team can share them.
Personal rules belong elsewhere
Your editor’s swap files and your operating system’s clutter are specific to you, so they do not have to live in every repository. Create one global file and register it with this command:
git config --global core.excludesFile ~/.gitignore_global
When a rule seems to have no effect, ask Git directly. Running git check-ignore -v path/to/file prints the exact file and line responsible, and prints nothing if no rule matches.
If a secret was already committed
Adding a file to .gitignore does not remove it from past commits. Anyone with access to the repository’s history can still read it. Treat an exposed key as compromised: revoke it and issue a new one first, then clean the history with a tool like git filter-repo if you need to.