Committed a .env file to GitHub? Do this first
By the Neuctra team. Updated
Revoke every key in that file right now, before you touch the repository. Deleting the file doesn't help, because the secret is still in your Git history. GitHub says that removing the secret, pushing a new commit, or even deleting and recreating the repository won't stop it being exploited.
Why you have to move fast
Stripe warns that bad actors continuously scan public repositories for API keys. So treat a pushed key as stolen, even if the repository was only public for a minute. Stripe also warns that private repositories can be exposed too.
Step 1: Revoke and replace every key
Go to each service and create a new key, then revoke the old one. Do this for everything in the file:
- Payment keys, like Stripe
- AI provider keys
- Database passwords and connection strings
- Cloud credentials
- Signing secrets and webhook secrets
Rotate first and clean up afterwards. GitHub says that revoking or rotating is the first step, and that once a secret is revoked, this may be all you need.
Step 2: Check whether the key was used
Look through each provider's logs and billing for activity you don't recognize, from when the key was pushed until you revoked it. For AI keys, a sudden jump in usage is the usual sign.
Step 3: Stop tracking the file
Add the file to .gitignore, then remove it from Git without deleting your local copy:
echo ".env*" >> .gitignore
git rm --cached .env
git commit -m "Stop tracking .env"
Step 4: Decide whether to rewrite history
If the keys are revoked, the old values in your history can no longer do any harm. Rewriting history is optional, and it's disruptive, because it means force-pushing and coordinating with everyone who has a clone. If the file also held something you can't rotate, like personal data, then it's worth doing. GitHub recommends git-filter-repo and explains the steps.
Step 5: Stop it happening again
- Add
.env*to.gitignorebefore your first commit in every new project. - Commit a
.env.examplefile with the names but no values. - Turn on GitHub secret scanning, which is free for public repositories.
- Use test keys in development, so a leak there costs you less.
Keep a copy of your keys outside the project
A .env file is meant for your app to read, not for keeping track of your secrets. If it's your only record, you end up with old copies across laptops and folders, with no idea which key is current or when it was last changed.
Notlix API Keys stores them encrypted on your device, grouped by project and environment, with reminders to rotate them. It imports a .env file in one step, and can download one when you need it. It's built for one developer, so if a team needs to share production secrets, a dedicated secrets manager is the better fit.
