Step 1: Revoke the API key immediately. Before anything else. Even if you fix the git history, crawlers may have already indexed the commit.
Step 2: Use git revert to create a new commit that undoes the changes:
git revert <commit-hash>
git push
This preserves history and is safe for shared branches. The sensitive data is still in history, but since the key is revoked it's no longer a live vulnerability.
Step 3: If you want to purge history (optional, for compliance): use git filter-repo (not filter-branch) and force-push. But this rewrites history and will require all collaborators to re-clone. Coordinate with your team.
pip install git-filter-repo
git filter-repo --path .env --invert-paths
git push --force-with-lease