Generate a .gitignore combining the rules for your language, framework, editor, and operating system, so build artifacts and local files stay out of the repository.
Generate a .gitignore file by selecting templates for languages, frameworks, IDEs, and operating systems. Templates are deduplicated and organized with section headers. Add custom rules at the bottom.
The rule that matters most is about secrets, and it is a rule about timing: .gitignore only prevents untracked files from being added. It does nothing about a file already committed. If a .env file has been committed once, adding it to .gitignore leaves it in history and fully retrievable, and the only correct response is to rotate every credential in it. History rewriting is optional; rotation is not.
The second-order habit is committing a .env.example with the keys and no values. It documents what configuration the project needs without any risk of the real values ending up in the repository.