Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
- Revoke / rotate the secret now (new key, new password) — assume it is already stolen.
- Clean the history in a fresh mirror clone, then force-push:
git clone --mirror git@github.com:ORG/shop-api.git
cd shop-api.git
git filter-repo --invert-paths --path config/.env
git filter-repo --replace-text ../replacements.txt
git push --force --mirror- Everyone deletes their old clone and clones again.
Step by step
- Revoke first. Cleaning Git takes time, bots scan public repos within minutes. Create a new key, deploy it, then disable the old one, and check the provider's logs for use of the old key.
- Find every place the secret is (all branches, all commits):
gitleaks git -v --redact
git log --all --oneline -- config/.env
git log --all -S 'AKIAEXAMPLEKEY' --oneline- Install git-filter-repo (the tool Git itself recommends;
git filter-branchis old and slow):
pip install --user git-filter-repo
dnf install -y git-filter-repo
apt-get install -y git-filter-repo- Work on a fresh mirror clone (all branches and tags; your normal working copy stays as a backup):
- Remove whole files, or replace the secret text everywhere:
git filter-repo --invert-paths --path config/.env --path secrets/prod.jsonAKIAEXAMPLEKEY==>***REMOVED***
regex:password\s*=\s*"[^"]+"==>password = "***REMOVED***"- Push the new history (needs force-push rights; turn off branch protection for a moment if needed):
git push --force --mirror- Clean up around it:
- All team members: delete old clones and clone again (an old clone can push the secret back).
- Open pull requests and forks still have the old commits — on GitHub, ask GitHub Support to remove cached views and PR refs; contact fork owners.
- CI caches, artifacts and container images built from the old commits may also contain the file.
- Stop it next time: a
.gitignorefor.env, a Gitleaks pre-commit hook, Gitleaks in CI, and GitHub push protection (Settings → Code security).
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Refusing to destructively overwrite repo history | filter-repo wants a fresh clone. | Use a new --mirror clone (or --force if you are sure). |
remote: error: GH006: Protected branch update failed | Branch protection blocks the force-push. | Allow force-push for a moment (admin), push, turn protection on again. |
| The secret is back after some days | A teammate pushed from an old clone. | Everyone re-clones; protect branches again. |
| Commit hashes changed, links in issues are broken | Rewriting history changes every later commit. | Expected — tell the team; old links can not be fixed. |
| Gitleaks still finds it in the fresh clone | Another branch or tag, or another file name. | git log --all -S; add the path or the text to the filter and run again. |
| The secret was in a public repo | Bots probably copied it already. | Rotation is the only real fix; cleaning only limits further spread. |
Go further
- Command pages: gitleaks, git.
- Next how-tos: Undo the last Git commit, Create a Kubernetes Secret safely, Sign Git commits, Find secrets with Gitleaks.
- Learn it: Git — Secrets, Git — Scan for secrets, Git — Hooks, Git — When a secret leaked.
- Full guides: Gitleaks, Vault, SOPS. At work: Gitleaks in the supply chain.
1A secret was pushed. What is the first step?
Revoke or rotate the secret — cleaning the history comes after.
2Why is a new commit that deletes the file not enough?
The secret is still in the old commits, in every clone and in forks.