Committed your .env file to GitHub? What to do, in order
git rm --cached .env, add .env to .gitignore and commit a .env.example with placeholders. Rewriting history with git filter-repo is optional cleanup; GitHub warns forks and cached views can keep the old commit.Why adding .env to .gitignore is not enough
Git's documentation says: "Files already tracked by Git are not affected" by .gitignore. Once .env is committed, git keeps tracking it and every later change to it, until you remove it from the index. And removing it today does not remove it from the commits that already exist.
GitHub's docs put rotation first: if you committed a secret, "then as a first step you need to revoke and/or rotate that secret." Its remediation tutorial adds that removing the secret, pushing a new commit, or deleting and recreating the repository does not prevent the secret from being exploited.
Fix it, in order
Stop tracking .env and ignore it from now on
git rm --cached .env
printf '.env*\n!.env.example\n' >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"
git push
Optional, after rotating: remove .env from history (on a fresh clone)
git filter-repo --sensitive-data-removal --invert-paths --path .env
git push --force --mirror origin
These are the flags GitHub's guide uses. git filter-repo is a separate install. GitHub also notes that commits that exist in forks stay accessible there, which is why rotation comes first.
- Open the committed
.envand list every value in it. Rotate each one at its provider: database passwords, Supabase secret or service_role keys, Stripe keys, OpenAI keys, webhook secrets, OAuth client secrets. - Put the new values in your host's environment settings and redeploy.
- Stop tracking the file and ignore it, with the commands below.
git rm --cachedremoves the file from the index and leaves your local copy alone. - Commit a
.env.examplewith the same names and placeholder values, so the next person knows what to set. - Decide whether to rewrite history. It is cleanup, not protection, and it changes every commit hash after the first appearance of the file. If the repository is public or shared widely, it is usually worth doing.
- If you rewrite history on GitHub, follow GitHub's guide to the end: it may involve contacting GitHub Support to remove cached views and dereference affected pull requests, and asking collaborators to re-clone.
Check it automatically
Caelo reports an env file that git tracks (env.committed) and one that no .gitignore rule excludes (env.not-ignored), and lists the secret-looking variable names it sets, never the values. To know whether a file is tracked, it reads .git/index as data; it never runs git. A git-ignored local .env is where secrets belong, so Caelo notes it and does not flag the values inside. It reads the working tree, not history, so pair it with a history scanner such as gitleaks once the file is gone.
Caelo output for an env file nothing ignores
- Env file with secret-looking values is not git-ignored
.env:1 (env.not-ignored)
value: (.env sets STRIPE_SECRET_KEY)
why: Git does not track this file today, but no .gitignore rule excludes it, so the next `git add .` commits it.
- Install Caelo: npx caelo . or the MCP server
- How Caelo compares with gitleaks, TruffleHog, Semgrep and others
1. Run the free local check
npx caelo .
Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and .env files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Act on a finding
Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.
For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.
Questions
My repository is private. Do I still need to rotate?
Yes, to be safe. Everyone with access, every clone, every CI system and every connected app has the values, and a private repository can be made public later.
Is it OK to commit .env.example?
Yes, with placeholder values only. It documents which variables the app needs. Caelo skips .env.example, .env.sample and .env.template files.
Can I just delete the repository and make a new one?
That does not un-leak anything. GitHub's docs say deleting and recreating the repository does not prevent the secret from being exploited. Rotate the secrets.
GitHub emailed me about a secret in the file. Is that the same thing?
Yes. That is secret scanning or a provider partner alert. See our guide on GitHub secret scanning alerts for what to do with the alert itself.