Where the rules come from
The templates follow the widely used community lists for each language and tool, trimmed of rarely needed lines. Build output, dependency folders, caches, logs, editor settings and operating system clutter such as .DS_Store and Thumbs.db are covered, and lines starting with ! keep files you usually want, like the Gradle wrapper or shared VS Code settings.
Keep secrets out of Git
Add Environment files and secrets to ignore .env files, private keys and credential files while keeping .env.example. A .gitignore only stops new files from being added. If a secret was already committed, remove it with git rm --cached, then rotate the key, because it stays in the history.
Using the file
Save it as .gitignore in the top folder of your repository, next to the .git folder. Patterns ending in / match folders, a leading / anchors a pattern to the repository root, ** matches any depth, and later rules win over earlier ones. Run git status --ignored to check what is being skipped.