Guide

Gitignore for .env Files: Keep Secrets Out of Git

Short answer: for a .gitignore that keeps .env files out of Git, pick your languages, frameworks, editors and operating systems in the Gitignore Generator and it combines the matching patterns into one .gitignore, each under a labelled section. Save it in the root of your repository and commit it. Then check the four things below, because a generic file cannot know your project.

Generating the file

The generator has 27 templates, covering languages (Node, Python, Java, Go, Rust, Ruby, PHP and more), frameworks (React, Next.js, Django, Laravel), editors, operating systems, Docker, Terraform, Unity and LaTeX. macOS, Windows and Linux start selected, because files such as .DS_Store and Thumbs.db should never be committed.

For example, Node and Python together produce:

### Node ###
node_modules/
npm-debug.log*
dist/
build/
coverage/
.env
.env.local
### Python ###
__pycache__/
*.py[cod]
.venv/
.pytest_cache/

Check these four things

1. Secrets

Only some templates (Node, React, Next.js, Laravel) list .env files. If your stack keeps secrets elsewhere, add that file yourself. If a secret was ever committed, rotating the secret matters more than deleting the file, because it stays in history. See Leaked API key: what to do, and scan text with the Secret Scanner. To read or convert an existing .env, use the .env ⇄ JSON Converter and read about .env quoting and multi-line values.

2. Lock files

The Rust template ignores Cargo.lock and the Dart template ignores pubspec.lock, which suits libraries. Applications usually commit them, so delete those lines for an app. The Dart template also ignores generated *.g.dart files: remove that line if you commit generated code.

3. Wrapper files

The Java template ignores *.jar but keeps gradle/wrapper/gradle-wrapper.jar and .mvn/wrapper/maven-wrapper.jar, so the build wrappers still work.

4. Order matters

A line starting with ! re-includes a file only if it comes after the pattern that ignored it. Do not sort or reorder the lines.

Files Git already tracks

A .gitignore does not affect files that Git already tracks. To stop tracking one, run:

git rm --cached path/to/file
git commit -m "Stop tracking file"

If you already have a .gitignore, paste both versions into the Diff Checker to see what the generated file would add.

A global ignore file

For operating-system and editor files you never want in any project, a global ignore file is cleaner: set it with git config --global core.excludesfile.

How to build a gitignore for .env files with the Gitignore Generator

  1. Open the Gitignore Generator and filter or select templates.
  2. Review the result, since some frameworks share overlapping patterns.
  3. Copy it or download it as .gitignore.
  4. Add your own local build folders and secret files, then commit.

Limits to know

  • The templates cover common cases and are not a byte-for-byte copy of any single source, including GitHub's own.
  • Nothing here knows your project-specific folders or secrets.
  • Everything is assembled in your browser.

Frequently asked questions

How do I ignore a file Git already tracks? Run git rm --cached on it and commit.

Should I ignore .env? Yes. Keep a .env.example without real values instead.

Should I commit lock files? Usually yes for applications, and often no for libraries (Rust, Dart). Check what your project needs.

Why is my ! rule ignored? It must come after the pattern that ignored the file.

How many templates are there? 27.

Related tools and guides