Rotate the leaked secret first, because rewriting history cannot take it back. Then remove it from every commit with git filter-repo and force push, knowing clones and forks may keep it.
The short answer
#Revoke or rotate the secret straight away. Assume it is compromised the moment it was pushed. Then, if you also want it out of the history, rewrite every commit with git filter-repo and force push.
_10# 1. Rotate the key with the provider first_10# 2. In a fresh clone, replace the secret in all history_10echo 'sk_live_abc123==>REMOVED' > ../replacements.txt_10git filter-repo --replace-text ../replacements.txt_10# 3. Re-add the remote and overwrite it_10git remote add origin [email protected]:you/project.git_10git push --force --all origin_10git push --force --tags origin
Why it happens
#Every commit stores a full snapshot of your files. Deleting the secret in a new commit only removes it from the latest snapshot; every older commit still contains it, and anyone with the repository can read it with git show or git log -p.
Once a commit is pushed, you also lose control of the copies. Other developers have cloned it, CI systems have checked it out, forks may exist, and automated scanners watch public repositories for keys within minutes. Rewriting history cleans the repository you control, but it cannot reach those copies. That is why rotating the secret always comes first: a revoked key is harmless wherever it ends up.
How to fix it
#Rotate the secret
Go to the provider (your cloud console, payment provider, database host and so on), revoke the leaked credential and issue a new one. Check the provider's logs for any use you do not recognise. Put the new value in an environment variable or secret manager, not in the code.
If you have not pushed yet
If the commit only exists locally, you do not need to rewrite anything shared. Remove the file or value, then amend the commit.
_10git rm --cached .env_10echo ".env" >> .gitignore_10git add .gitignore_10git commit --amend --no-edit
If the secret is in an older local commit, use git rebase -i to edit that commit, or use git filter-repo as below before pushing. Rotating is still wise if there is any chance it left your machine.
Remove it from all history with git filter-repo
git filter-repo is the tool the Git project recommends in place of the older git filter-branch. Install it from your package manager (for example brew install git-filter-repo or pip install git-filter-repo).
Work in a fresh clone. By default filter-repo refuses to run in a repository that is not a fresh clone, so you do not destroy local work by mistake.
_10git clone [email protected]:you/project.git project-clean_10cd project-clean
To replace the secret text wherever it appears, list it in a file. Each line is a literal string, and ==> sets the replacement. Keep this file outside the repository.
_10printf 'sk_live_abc123==>REMOVED\n' > ../replacements.txt_10git filter-repo --replace-text ../replacements.txt
To remove a whole file from every commit instead, use --invert-paths.
_10git filter-repo --invert-paths --path .env
The rewrite changes the hash of every affected commit and everything after it. filter-repo also removes the origin remote as a safety measure, so add it back before pushing.
Force push and tell everyone
_10git remote add origin [email protected]:you/project.git_10git push --force --all origin_10git push --force --tags origin
Branch protection may block force pushes, so an admin may need to allow it briefly. Then ask everyone to delete their old clones and clone again. If someone merges or pushes from an old clone, the secret comes straight back.
On hosted platforms, old commits can still be reachable through pull request refs or caches. On GitHub, for example, you may need to contact support to purge them. Forks are separate repositories and keep the old history.
Stop it happening again
- Add
.envand similar files to.gitignorebefore you create them. - Turn on secret scanning and push protection if your host offers it.
- Run a scanner such as gitleaks in a pre-commit hook or in CI.
Common pitfalls
#- Rewriting history but not rotating. The key is already exposed. Rewriting alone gives a false sense of safety.
- Deleting the file in a new commit and stopping there. The secret is still in older commits.
- Keeping the replacements file in the repo. It contains the secret. Store it outside and delete it afterwards.
- Teammates pushing from old clones. One push undoes the whole clean-up. Make sure everyone reclones.
- Forgetting tags and other branches. Push with
--alland--tags, or the secret survives there.
Related reading
#Was this resource helpful?