Guide
How to keep .env out of git, and what to do if you committed it
A committed `.env` is one of the most common ways secrets leak. Here’s how to make sure git never picks it up, and exactly what to do if it already has.
By Anmol Sethi · Updated
01
Ignore every .env, keep the example
Add these lines to the project’s .gitignore:
.env
.env.*
!.env.exampleThat ignores .env, .env.local, .env.staging and friends, but keeps .env.example so teammates know which keys exist.
02
Check that git really ignores it
git check-ignore -v .envIt prints the rule that matches, like .gitignore:1:.env .env. No output means .env is not ignored.
.gitignore doesn’t affect files git already tracks. If .env was committed before, it stays tracked until you untrack it (next step).
03
Already committed? Untrack it
git rm --cached .env
git commit -m "Stop tracking .env"This keeps your local file and removes it from future commits. The old commits still contain it.
04
Rotate the keys first
If the commit was ever pushed, treat every secret in it as leaked: anyone with a clone already has them. Create new API keys, database passwords and tokens, and revoke the old ones. Rewriting history doesn’t undo a leak; rotating does.
05
Remove it from history
To scrub the file from every commit, use git filter-repo (brew install git-filter-repo) on a fresh clone:
git filter-repo --invert-paths --path .env
git remote add origin <your-repo-url> # filter-repo removes origin
git push --force --allEveryone else must re-clone afterwards, and forks or cached copies on your git host may still have the old commits. That’s why rotating comes first.
06
Ignore .env in every repo
A global ignore file covers projects that forgot:
echo ".env" >> ~/.gitignore_global
git config --global core.excludesFile ~/.gitignore_global07
The one-click way: Dotvane
Dotvane runs git check-ignore for every project you add. If .env would be committed, the project shows “.env isn’t in .gitignore” and the menu bar icon turns into a warning, before you push.
And because profiles live in the Keychain, the only secret file in your repo folder is the active .env. Try it free.
Try Dotvane free for 7 days. Every feature, no account.
Download for Mac