Short answer
Deleting a key from your code does not remove it from git: every earlier commit still contains it, in every clone and fork. Treat any secret that was ever committed as leaked. Find them with git log, gitleaks, TruffleHog or GitHub secret scanning, rotate each one first (new key in, old key revoked), check for misuse, and only then decide whether rewriting history with git filter-repo is worth it. Rewriting alone never makes a leaked key safe again.
Ask an AI about this page:ChatGPTClaude
On this page
Key takeaways
- A secret that was ever committed is leaked, even if the next commit deleted it.
- Rotate first: create a new key, deploy it, then revoke the old one. Cleaning history comes later, if at all.
- Find old secrets with
git log, gitleaks, TruffleHog or GitHub secret scanning, which scans every branch’s full history. - Rewriting history with
git filter-repodoesn’t reach clones, forks or cached views. GitHub says so itself. - Prevent the next one: only public values in committed files, push protection on, server secrets in Edge Function secrets.
Why deleting the file does not help
Git stores every version of every file. When you commit a .env file and delete it a minute later, you have two commits: one that adds the file with your key, and one that removes it. The second one doesn’t change the first.
We reproduced this in a small test repository with a made-up key: commit a .env file, delete it and add it to .gitignore in the next commit. The file is gone from the working tree, git ls-files no longer lists it, and still this command prints the key in full:
git log --all -p -G 'sk-|sb_secret_|service_role' --onelineThe output shows both commits: the one that added the line and the one that removed it, each with the key in plain text. Anyone with a clone can run the same command. That includes everyone who cloned before you deleted the file, every fork, and every tool or integration with read access to the repository.
Step 1: find what leaked
Start with plain git. These two commands need nothing installed and search every branch:
git log --all --oneline --diff-filter=A -- '*.env' '.env*'The -G command above finds lines that match a pattern. Adjust the pattern to the keys your app uses: sk- for OpenAI-style keys, sb_secret_ for Supabase secret keys, service_role for the legacy Supabase key, whsec_ for webhook secrets and so on.
For a thorough search, use a scanner that knows hundreds of key formats:
- Gitleaks scans the history with
gitleaks git -v(it runsgit log -punderneath) and a folder withgitleaks dir -v.--redacthides the secret values in the output, which is useful if you share the report. - TruffleHog scans a local repository with
trufflehog git file://. --results=verified,unknown. “Verified” means TruffleHog checked with the provider’s API that the key still works, which tells you which leaks are live. Run it only on your own repositories. - GitHub secret scanning runs automatically and for free on public repositories and, according to GitHub, scans “your entire Git history on all branches”. For private repositories in an organisation it needs GitHub Secret Protection. Alerts appear in the repository’s security tab.
Make a list: which secret, which commit, since when, and who could see the repository during that time.
Step 2: rotate every secret on the list
Rotation means: create a new secret, put it everywhere the old one was used, check that everything works, then revoke the old one. In that order, so your app keeps running. Supabase’s guide adds a step before all of this: fix the cause of the leak first, or the new key leaks the same way.
| Secret | Rotate it here | Notes |
|---|---|---|
Supabase secret key (sb_secret_…) | Dashboard → Settings → API Keys: create a new secret key, update every function and server, then delete the old one | Supabase warns that deleting a secret key can’t be undone |
Supabase legacy service_role key | Move to a new secret key, then deactivate the legacy keys and revoke the legacy JWT secret | Deactivating also disables the legacy anon key, so move browser clients to a publishable key first |
| OpenAI API key | Create a new key, update your Edge Function secret, revoke the old key | OpenAI’s key safety guide: rotate immediately if you suspect a leak |
| Any other provider (Stripe, Resend, webhook secrets) | The provider’s API key or webhook settings | Same order: new key in, old key out |
For the full first hour after a leaked Supabase key, follow Leaked service_role key: the first 60 minutes. For how rotation works in general, see secret rotation.
Step 3: check whether someone used it
After rotating, look back. Most providers show usage per key or per project: unexpected spend on your AI provider, API calls from unknown places, new users or changed data in your database during the window the key was exposed. If you find misuse, you are dealing with an incident, not just a cleanup: note what happened and when, and tell affected users if their data was involved.
Step 4 (optional): rewrite the history
Once every leaked secret is rotated, the old values are useless. Rewriting history is then mostly cosmetic, and GitHub recommends it mainly for data you can’t rotate, such as personal data. If you still want to remove the file:
git-filter-repo --sensitive-data-removal --invert-paths --path .env
git push --force --mirror originGitHub’s guide lists what this does not reach and what it breaks:
- Clones and forks keep the old history. Anyone who pulls old commits back in and pushes can bring the secret back.
- Cached views and pull request references can still show the old commits by their hash; for those you have to contact GitHub Support.
- Every commit hash after the change is new, open pull requests can break, and commit signatures are lost.
- The force push rewrites history for everyone, including any tool that syncs with your repository. Coordinate with collaborators first.
That is why rotation comes first and rewriting is optional.
Prevent the next leak
Keep committed files public
Add files that hold real secrets, such as .env.local and supabase/functions/.env, to .gitignore, and commit an .env.example with placeholders instead. Lovable projects commit a .env on purpose: it holds only the Supabase URL and publishable key, which are public. Make sure nothing else ends up there.
Keep secrets out of the frontend
Anything in a VITE_ variable ends up in the browser bundle. Server secrets belong in Edge Function secrets. See Secret key in a VITE_ variable.
Turn on push protection
GitHub’s push protection for users is on by default and blocks pushes that contain recognised secrets to public repositories. For repository-level push protection, which also covers private repositories, an admin has to enable it, and it requires GitHub Secret Protection.
Scan before you commit
Gitleaks documents a pre-commit hook for the pre-commit framework. It catches a key on your machine, before it ever reaches a remote.
A fix prompt for your AI builder
Your AI builder can’t rotate keys for you, but it can find hard-coded secrets in the current code and move them to the right place:
Search the whole project for hard-coded secrets: API keys, Supabase secret or service_role keys, webhook secrets, passwords and tokens in source files, config files and VITE_ variables. Ignore the Supabase URL and the anon or publishable key.
For each one:
- replace it in server code (Supabase Edge Functions) with Deno.env.get(“NAME”) and tell me the secret name to set in Edge Function secrets;
- remove it from frontend code and VITE_ variables entirely and move the call that needs it into an Edge Function;
- make sure any committed .env file holds only public values (Supabase URL, anon or publishable key), add .env.local and supabase/functions/.env to .gitignore, and create an .env.example with placeholders for the rest.
Do not print the secret values. Finish with a list of every secret you found (name and file only) so I can rotate them with the provider.
Frequently asked questions
I deleted the .env file in the next commit. Am I safe?
No. The commit that added the file still exists, and anyone with a clone, a fork or access to the repository can read it with git log. We reproduced this in a test repository: after the file was deleted and ignored, git log still printed the key. Rotate the key.
My repository is private. Do I still need to rotate?
Yes, if the key could be used by anyone who has or had access: collaborators, tools and integrations connected to the repository, and anyone who gets access later. GitHub notes that private repositories can be breached too. Rotation is cheap; finding out later is not.
Is rewriting history with git filter-repo enough?
No. GitHub’s guide lists what survives a rewrite: other people’s clones and forks, cached views and pull request references that still point to the old commits. GitHub itself says to revoke or rotate a secret first and to rewrite history mainly for data that cannot be rotated.
Do I need to rotate the Supabase anon or publishable key?
No. That key is meant to be public and ships in your frontend anyway; what it can reach is decided by your row level security. Rotate the secret key (sb_secret_…) or the legacy service_role key, which bypass RLS.
Sources
- 1.Removing sensitive data from a repository — GitHub Docs
- 2.About secret scanning — GitHub Docs
- 3.About push protection — GitHub Docs
- 4.Gitleaks — Gitleaks on GitHub
- 5.TruffleHog — Truffle Security on GitHub
- 6.Rotate a leaked or compromised API key — Supabase Docs
- 7.Best Practices for API Key Safety — OpenAI Help Center