You committed a .env file. A minute later you deleted it, committed the deletion, and told yourself it was handled. It was not. The file still exists in the commit where you added it, and anyone who can clone the repo can read it. Deleting a file in a later commit hides it from the working tree, not from the repository.
This is a repair job, not a cleanup job. Here is the order that works: rotate the exposed keys, find every commit that contains the file, rewrite history with git filter-repo, force-push with your team coordinated, verify, then fix the gap that let it happen.
Step 1: Rotate the exposed keys first
Assume the contents are already compromised. History rewriting removes the file from your remote, but it cannot remove the copy someone pulled, the fork that already exists, or a value that a bot scraped. Rotation is the only step that actually protects you, so do it before you touch Git.
Rotate in order of blast radius: anything that can spend money (payment keys, cloud provider credentials), anything that can read or write customer data (database URLs, object storage keys), then everything else (third-party API tokens, webhook signing secrets). Revoke the old value rather than just issuing a new one. A key you rotated but left active is still a key.
If the file carried a DATABASE_URL to production, check the provider's access logs for the window between the first commit and now. Rotation closes the door. The logs tell you whether anyone walked through it.
Step 2: Find every commit that touched the file
Search all refs, not just your current branch. The file is often on a feature branch you forgot about, or in a tag you pushed months ago.
git log --all --full-history -- .env
git log --all -p -- .env | grep -E "^\+[A-Z_]+="
The first command lists the commits. The second prints added lines that look like variable assignments, which is how you inventory what actually leaked. Run it per path if your file lives in a subdirectory, such as services/api/.env, and check .env.local, .env.production, and any similarly named siblings.
Step 3: Rewrite history with git filter-repo
git filter-branch is deprecated and slow on most repos. Use git filter-repo. Work from a fresh mirror clone so you are not rewriting the copy you are still actively pushing from, and keep a full backup of the original before you start.
git clone --mirror git@github.com:your-org/your-repo.git
cd your-repo.git
git filter-repo --path .env --invert-paths --force
The --invert-paths flag means remove this path from every commit. To catch variants in one pass, pass several:
git filter-repo --invert-paths \
--path .env \
--path .env.local \
--path-glob '*.env' \
--force
Be precise with globs. A pattern wide enough to match a legitimate config file will rewrite commits that had nothing to do with the leak, and that makes the rest of the repair harder to verify.
If your team cannot install filter-repo, BFG Repo-Cleaner does the same class of rewrite with bfg --delete-files .env. Both tools change every commit hash after the first affected commit. That is expected, and it is why the next step needs coordination.
Step 4: Force-push and coordinate the rewrite
Tell the team to stop pushing before you push. Announce a window, even a short one. Anyone who pushes from a stale clone during the rewrite will reintroduce the old commits, and you will be doing this again.
git push --force
From a mirror clone, that pushes all refs: branches, tags, and notes. Confirm both branch lists afterward. Tags are the usual miss, since many people delete the file from main and leave the tag that references the old tree intact.
Then have every collaborator re-clone. A git pull on a rewritten history produces a merge of two unrelated pasts, and any local commits pushed from that state brings the old objects back. Re-cloning is faster than explaining what went wrong. Open pull requests based on deleted branches need to be reopened against the new history.
Step 5: Verify the file is really gone
Check the remote, not your local copy.
git rev-list --objects --all | grep -i '\.env'
git log --all -- .env
Both should return nothing. If a path still shows up, a tag or remote branch was missed, and the fix is another filter-repo pass with the same paths.
Check the surrounding surfaces too. Forks and mirrors keep their own copies of the old objects, so ask owners to delete them or sync them from the rewritten origin. CI logs may have printed the file during a failed build. Hosting providers cache commit views for a while, and most of them will purge those caches on request for a verified secret exposure.
Step 6: Prevent the next commit
Rewrite the ignore rules so the file cannot be staged by accident:
.env
.env.*
!.env.example
Then add a pre-commit hook that checks the staged diff for secret patterns before the commit exists. gitleaks and detect-secrets both run as pre-commit hooks and both fail the commit rather than warning about it. A warning gets clicked past. A failed commit gets read.
The real gap is usually not the hook. It is that the file has to exist at all, on every machine, for every environment, so it gets passed around and eventually committed. The durable fix is to stop distributing secrets as files: pull the values your process needs at runtime from one encrypted source of truth, with access scoped per person and per environment. That is what Envly does, and it removes the .env file from the workflow rather than guarding it.
The short version
Rotate first, because the leak already happened. Find every commit that touched the file across all refs. Rewrite with git filter-repo, force-push during a freeze, and make everyone re-clone. Verify on the remote, including forks and tags. Then close the gap that let a secrets file sit in your project root in the first place.
