Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 43Security · Git

How to remove a secret from Git history

An API key, a password or a .env file was committed and pushed. Deleting the file in a new commit is not enough — the secret stays in every old commit, every clone and every fork. The right order is: 1. revoke the secret, 2. clean the history, 3. stop it from happening again. This how-to does all three with git filter-repo and Gitleaks, including the GitHub-specific steps.

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

43.1

The quick answer

  1. Revoke / rotate the secret now (new key, new password) — assume it is already stolen.
  2. 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
  1. Everyone deletes their old clone and clones again.

Commando's in deze les

43.2

Step by step

  1. 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.
  2. 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
  1. Install git-filter-repo (the tool Git itself recommends; git filter-branch is old and slow):
pip install --user git-filter-repo
dnf install -y git-filter-repo
apt-get install -y git-filter-repo
  1. Work on a fresh mirror clone (all branches and tags; your normal working copy stays as a backup):
git clone --mirror git@github.com:ORG/shop-api.git
cd shop-api.git
  1. Remove whole files, or replace the secret text everywhere:
git filter-repo --invert-paths --path config/.env --path secrets/prod.json
../replacements.txt
AKIAEXAMPLEKEY==>***REMOVED***
regex:password\s*=\s*"[^"]+"==>password = "***REMOVED***"
git filter-repo --replace-text ../replacements.txt
gitleaks git -v --redact
  1. Push the new history (needs force-push rights; turn off branch protection for a moment if needed):
git push --force --mirror
  1. 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.
  1. Stop it next time: a .gitignore for .env, a Gitleaks pre-commit hook, Gitleaks in CI, and GitHub push protection (Settings → Code security).

43.3

Check that it worked

git clone git@github.com:ORG/shop-api.git /tmp/check && cd /tmp/check
gitleaks git -v --redact
git log --all -S 'AKIAEXAMPLEKEY' --oneline | wc -l
0

A fresh clone has no finding, and the old key is disabled at the provider.

43.4

Common problems

ProblemCauseFix
Refusing to destructively overwrite repo historyfilter-repo wants a fresh clone.Use a new --mirror clone (or --force if you are sure).
remote: error: GH006: Protected branch update failedBranch protection blocks the force-push.Allow force-push for a moment (admin), push, turn protection on again.
The secret is back after some daysA teammate pushed from an old clone.Everyone re-clones; protect branches again.
Commit hashes changed, links in issues are brokenRewriting history changes every later commit.Expected — tell the team; old links can not be fixed.
Gitleaks still finds it in the fresh cloneAnother 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 repoBots probably copied it already.Rotation is the only real fix; cleaning only limits further spread.

Commando's in deze les

43.5

Go further

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.

Alle commando's in dit hoofdstuk