Finding and Removing Secrets from Git History: gitleaks and git filter-repo
Deleting a password from a file and committing the change does not remove it from git history. Even in a private repository, one leaked token that can read the repository leaks the whole history with it. So the first question is not “is there a secret in the code now” but “was there ever a secret anywhere in the history”.
This post walks through scanning the history of a few dozen personal repositories and removing secrets from the history of one of them. The tools are gitleaks 8.30.1 and git filter-repo. The focus is less on tool usage and more on the three traps that actually caught me.
At a glance
Section titled “At a glance”Order matters. Rewriting history comes last.
| Step | What you do | Key point |
|---|---|---|
| 1. Find | Scan the whole history with gitleaks | Never print the values |
| 2. Rotate | Revoke each value at its issuer and issue a new one | Rewriting history does not undo exposure |
| 3. Remove | Drop paths or values from history with git filter-repo | The latest code must not change |
| 4. Verify | Compare tree hashes, scan again | Check with numbers |
| 5. Publish | Force push, clean up other copies | Old commits stay on GitHub for a while |
1. Find secrets across the whole history
Section titled “1. Find secrets across the whole history”Scan a mirror clone
Section titled “Scan a mirror clone”Use a --mirror clone, which fetches every branch, tag and commit without a working tree. It leaves your working copy alone and also covers values that only ever lived on other branches.
git clone --mirror https://github.com/<owner>/my-gitops.git my-gitops.gitgitleaks git my-gitops.git --redact --report-format json --report-path report.jsongitleaks git scans the changes in every commit. With --redact, values are masked in the report and the logs. The report still keeps the rule name (RuleID), file, line, commit and date, which is enough to locate each finding.
When you scan many repositories at once, group the results by repository, file and key name. The same value is reported again for every commit that touches it. This time there were 803 findings, which collapsed to 281 distinct values.
In the latest code, or only in history?
Section titled “In the latest code, or only in history?”For each value, record whether it is also in the latest code (HEAD). The next step differs.
- In HEAD as well: take the value out of the code and move it to a secret manager.
- Only in history: rotate it, then decide whether to remove it from history.
Filter out false positives
Section titled “Filter out false positives”The generic-api-key rule produces many false positives. The ones I filtered out were minified JavaScript libraries (*.min.js), example Secrets that ship with Helm charts, a document listing image digests, and sample JWTs in test code. For a JWT, decoding only the exp claim of the payload tells you right away whether it has expired.
Trap 1: values found by decoding base64
Section titled “Trap 1: values found by decoding base64”The data: values in a Kubernetes Secret manifest are base64-encoded. gitleaks decodes encoded values and scans them again. The --max-decode-depth option controls this, and its default is 5.
So the value in the report may differ from the string actually written in the file. That is why searching the file for the reported value finds nothing. In that case, open the line the report points to and work with the original encoded string.
2. Rotate before you rewrite
Section titled “2. Rotate before you rewrite”GitHub’s documentation also lists revoking and rotating the value as the first step. It says that if rotation is enough, rewriting history may not be warranted. If someone has already taken a copy, rewriting history does not remove it. Revoking the value, on the other hand, makes every remaining copy useless.
Rewriting history keeps the repository from leaking the value again. It does not undo the exposure.
3. Remove with git filter-repo
Section titled “3. Remove with git filter-repo”git filter-repo is the tool GitHub’s documentation recommends. A rewrite cannot be undone, so work in a freshly fetched mirror and take a backup first with git bundle create backup.bundle --all. The backup still contains the secrets, so delete it when you are done.
Remove a whole path
Section titled “Remove a whole path”If the directory is no longer needed, dropping the path from history is the simplest option.
git filter-repo --invert-paths --path overlays/prod-old --force--force skips the “is this a fresh clone” check that filter-repo performs by default. A mirror you just fetched is fine.
Trap 2: old values survive in a renamed directory
Section titled “Trap 2: old values survive in a renamed directory”In this repository, overlays/prod-old had at some point been renamed to overlays/prod. After removing the old path, a new scan found the old values again, now in the history of overlays/prod.
The rename commit explains it. In that commit, the files under overlays/prod are recorded as newly created, still holding the old values. Once only the old path is removed, that commit becomes “files with the old values added at the new path”. GitHub’s documentation also says that if the file ever lived at another path, you must name that path too. But the new path is still in use, so it cannot be dropped wholesale.
Replace values instead
Section titled “Replace values instead”In that case, remove values rather than paths. --replace-text replaces the expressions listed in a file across every text file in history.
literal:old-db-password-1234==>***REMOVED***literal:c2stZXhhbXBsZS1rZXkx==>***REMOVED***git filter-repo --replace-text replace.txt --forcerm replace.txtA few rules I followed:
- List only values that are not in the latest code. Listing a value that is in HEAD changes the current code. Values in HEAD are handled separately by taking them out of the code.
- List longer values first. If one value contains another and the shorter one is replaced first, the longer one survives.
- For base64-encoded values, list the encoded string. Same reason as trap 1.
- Delete the replacement file right after use. It is itself a list of secrets.
After a replacement, a new scan can surface values that were not reported before, such as multi-line values or values matched by another rule. Repeat scan and replace until no history-only values remain.
4. Verify
Section titled “4. Verify”The latest code did not change
Section titled “The latest code did not change”A rewrite changes every commit hash, but the tree hash of HEAD must stay the same. That is the proof that the latest code was not touched.
git rev-parse HEAD^{tree} # compare with the value recorded before the rewriteNothing remains in history
Section titled “Nothing remains in history”git log --all --name-only --format= | grep -c 'overlays/prod-old' # must be 0gitleaks git . --redact --report-format json --report-path after.jsonCheck that the new scan reports zero values that are absent from HEAD. If findings remain, check whether they are the same as a value in HEAD, only in a different encoding.
5. Publish and clean up
Section titled “5. Publish and clean up”Force push
Section titled “Force push”git push --force origin refs/heads/main:refs/heads/mainIf a branch protection rule blocks force pushes, lift it temporarily. Open pull requests will lose their diffs, so merge or close them first.
Clean up other copies
Section titled “Clean up other copies”Old copies still hold the secrets after the rewrite. Move your own working copy to the new history and remove the old objects.
git fetch origingit reset --hard origin/maingit reflog expire --expire=now --allgit gc --prune=nowYou cannot clean other people’s clones or forks. Ask everyone who has a copy to clone again.
Old commits that stay on GitHub
Section titled “Old commits that stay on GitHub”According to GitHub’s documentation, after a force push the old commits can still be reached through cached views by SHA and through pull requests that reference them. Removing those completely requires a request to GitHub Support. The documentation also says Support only helps when rotating the credentials cannot mitigate the risk. If you no longer need the repository, deleting it is another option.
Limitations
Section titled “Limitations”- gitleaks matches known patterns. It can miss secrets in unusual formats and personal data such as names and phone numbers.
- Rewriting history only prevents future exposure. Copies that were already taken cannot be removed, which is why rotation comes first.
- Files stored with Git LFS and binary files are not covered by
--replace-text. - The commands here are based on gitleaks 8.30.1 and the git filter-repo version current when this was written. GitHub’s current documentation recommends adding the
--sensitive-data-removaloption, so follow that procedure if you are starting fresh.
References
Section titled “References”- Removing sensitive data from a repository, GitHub Docs
- gitleaks
- git filter-repo
- Managing secrets outside the code is covered in Platform Secret Management.