devlk
c/learning u/devlk Team

I pushed a password or API key to GitHub. What should I do?

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?

1
1 comment
No account needed: you'll get a random guest name, kept on this device. Or sign up to choose yours.
Sort by: Best Top New Old
devlk Team·

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.example with 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:

git filter-repo --invert-paths --path .env
git push --force --all

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:

  • Add .env, key files and credentials to .gitignore before your first commit.
  • Keep GitHub's secret scanning push protection on. It blocks many known key formats at push time.
  • Run git status and 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.

1