No, a .env file should not be committed to Git: it holds the secrets and machine-specific settings that Git history would keep and share forever. Add .env to .gitignore, and commit a .env.example with the same variable names and placeholder values instead.
The maintainers of dotenv, the library that popularised the file, answer the question in their FAQ with a single word: "No." (with one exception for files encrypted by their own tool, covered below). Every value in this article is a fake example.
Why .env doesn't belong in Git
- Git history is permanent and copied. Every clone, fork, CI cache and backup gets the full history. Deleting
.envin a later commit does not remove it from earlier ones. - Access to code is not access to secrets. Contractors, CI services and code-review tools need the code, not your production database password. OWASP's Secrets Management Cheat Sheet treats plaintext secrets in source code as a core problem to remove.
- Values differ per machine. Your local database URL, debug flags and test API keys are not your colleague's, and not production's. A committed
.envoverwrites everyone's settings on every pull. - Leaks are found quickly. Public repositories are scanned for credentials, which is why GitHub built push protection: it is "designed to prevent hardcoded credentials, such as secrets or tokens, from ever being pushed to your repository".
The setup: .gitignore plus .env.example
A typical .env (fake values):
DATABASE_URL=postgres://app:example-password@localhost:5432/app
STRIPE_SECRET_KEY=sk_test_FAKE_000000000000
The matching .env.example, which you do commit:
DATABASE_URL=postgres://user:password@localhost:5432/app
STRIPE_SECRET_KEY=
Then add these lines to .gitignore:
.env
.env.*
!.env.example
The second line ignores variants such as .env.local and .env.production; the ! line re-includes the example file. Check what Git will do with each file:
git check-ignore -v .env .env.local .env.example
Output from a test repository:
.gitignore:1:.env .env
.gitignore:2:.env.* .env.local
.gitignore:3:!.env.example .env.example
Each line shows the rule that matched. The ! rule on the last line means .env.example is not ignored. git status now lists only .env.example and .gitignore as new files, and an accidental git add .env is refused:
The following paths are ignored by one of your .gitignore files:
.env
hint: Use -f if you really want to add them.
If your framework uses a different naming scheme, follow it. Vite, for example, loads .env, .env.local, .env.[mode] and .env.[mode].local, and its docs say the *.local files should be git-ignored. Some teams commit a non-secret .env with defaults and keep secrets in .env.local; that works only if the committed file never gains a real secret.
Front-end projects: .env doesn't keep secrets out of the browser
Ignoring the file keeps it out of Git, but build tools can still publish its values. Vite exposes every variable prefixed with VITE_ to client code and bakes the value into the bundle at build time. Its documentation says VITE_* variables "should not contain sensitive information such as API keys". Anything a browser needs is public by definition; real secrets belong on a server.
If .env is already committed
First, stop tracking it while keeping your local copy:
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"
Adding .env to .gitignore alone does nothing for a file that is already tracked: in our test repository, git status still showed M .env (tracked and modified) after .env was added to .gitignore. Git's own documentation says it plainly: "Files already tracked by Git are not affected". (Gitignore not working? covers this and other causes.)
But the old contents are still in history. In the same test repository, after the commit above, git show <first-commit>:.env still printed API_KEY=fake-123. So:
- Not pushed yet: amend or rewrite the local commits so the file never leaves your machine.
- Pushed, or you aren't sure: treat every secret in the file as compromised. Rotate them first: revoke the old API keys, change the passwords, issue new tokens. OWASP's remediation advice is that exposed keys "should undergo immediate revocation", followed by issuing new secrets.
- Then clean history if you need to, with
git filter-repo. How to remove a secret from Git history walks through it. Rewriting history doesn't help on its own: anyone who cloned, and any service that cached the repository, still has the old commits.
Rotation is the step that actually stops misuse. A history rewrite without rotation leaves working credentials in every existing copy.
Where the real values should come from
- Local development: each developer's own
.env, created from.env.example. - CI: the CI system's encrypted secrets settings, injected as environment variables at run time.
- Production: the hosting platform's environment settings or a secrets manager. OWASP prefers injecting secrets as files or in memory over environment variables where possible, noting that environment variables "are generally accessible to all processes and may be included in logs or system dumps".
- Docker: when no
--env-fileis given, Compose "searches for an.envfile in the project directory" and uses it for interpolation, and the same rule applies: ignore it in Git, and also add it to.dockerignoresoCOPY . .doesn't bake it into an image. OWASP warns that secrets "should never be hardcoded using docker ENV or docker ARG commands". See Docker Compose explained.
Note that dotenv does not override variables that are already set; its FAQ says "By default, we will never modify any environment variables that have already been set." That lets the same code read a local .env in development and real platform-provided variables in production.
Stop it happening again
Put .env in .gitignore on the first commit of every project, before the file exists. Add a secret scanner as a pre-commit hook; OWASP recommends "enabling secrets detection at the developer level", including via pre-commit hooks. On GitHub, user-level push protection is enabled by default and "Stops you from pushing secrets to public repositories on GitHub"; repository-level push protection is disabled by default and has to be turned on by a repository administrator or organisation owner. When you need a fresh strong value for a password or signing secret, generate it rather than inventing one, for example with larely's password generator.
Frequently asked questions
- Is it OK to commit .env if the repository is private?
- It is still a bad idea. Everyone with read access, every fork and clone, every CI system and backup holding the repository gets the secrets, and they stay in the history forever. Private repositories also become public by mistake. Keep secrets out of Git regardless of visibility.
- What should go in .env.example?
- Every variable name the app needs, with placeholder or safe local-development values and a short comment where the format isn't obvious. Never real keys, passwords or tokens. A new developer should be able to copy it to .env, fill in their own values, and run the app.
- Can I commit an encrypted .env file?
- Some tools are designed for that. The dotenv FAQ says not to commit .env 'unless you encrypt it with dotenvx', and OWASP accepts encrypted secrets in Git only if developers cannot decrypt them themselves. The decryption key then becomes the secret to protect, and it must never be committed.
- Does adding .env to .gitignore remove it from the repository?
- No. .gitignore only affects untracked files. If .env was already committed, Git keeps tracking it. Run git rm --cached .env to stop tracking it while keeping your local copy, and treat any real secrets in it as leaked.