Team FAQ: this question comes up often, so we've answered it below. Add your own experience, or a different view, in the comments.
You accidentally committed a .env file, a database password or an API key, and it's now on GitHub. Is deleting the file enough?
No. Deleting the file in a new commit doesn't remove it from history. Treat the secret as stolen and act in this order.
1. Revoke or rotate the secret immediately. This is the step that actually protects you. Generate a new key or password in the provider's dashboard (AWS, Stripe, Google Cloud, your database) and disable the old one. Bots scan public GitHub for keys within minutes, so do this first, even before cleaning the repo.
2. Check for misuse. Look at billing, access logs and unexpected resources (for example new cloud servers mining crypto).
3. Stop tracking the file and ignore it:
git rm --cached .env echo ".env" >> .gitignore git commit -m "Stop tracking .env"Commit a
.env.examplewith placeholder values instead, so others know which variables are needed.4. Optionally, remove it from history. Use git filter-repo (or the BFG Repo-Cleaner), then force-push:
This rewrites history, so collaborators must re-clone. Forks and existing clones still have the old data, which is why step 1 matters more than this one.
Prevent it next time:
.env, key files and credentials to.gitignorebefore your first commit.git statusand read the list before every commit.Has a leaked key ever cost you (or someone you know) money? Share what happened so others can learn.