Skip to content

Finding and Removing Secrets from Git History: gitleaks and git filter-repo

Interactive Diagram

새 탭 ↗

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.

Order matters. Rewriting history comes last.

StepWhat you doKey point
1. FindScan the whole history with gitleaksNever print the values
2. RotateRevoke each value at its issuer and issue a new oneRewriting history does not undo exposure
3. RemoveDrop paths or values from history with git filter-repoThe latest code must not change
4. VerifyCompare tree hashes, scan againCheck with numbers
5. PublishForce push, clean up other copiesOld commits stay on GitHub for a while

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.

Terminal window
git clone --mirror https://github.com/<owner>/my-gitops.git my-gitops.git
gitleaks git my-gitops.git --redact --report-format json --report-path report.json

gitleaks 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.

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.

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.

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.

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.

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.

If the directory is no longer needed, dropping the path from history is the simplest option.

Terminal window
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.

In that case, remove values rather than paths. --replace-text replaces the expressions listed in a file across every text file in history.

replace.txt
literal:old-db-password-1234==>***REMOVED***
literal:c2stZXhhbXBsZS1rZXkx==>***REMOVED***
Terminal window
git filter-repo --replace-text replace.txt --force
rm replace.txt

A 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.

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.

Terminal window
git rev-parse HEAD^{tree} # compare with the value recorded before the rewrite
Terminal window
git log --all --name-only --format= | grep -c 'overlays/prod-old' # must be 0
gitleaks git . --redact --report-format json --report-path after.json

Check 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.

Terminal window
git push --force origin refs/heads/main:refs/heads/main

If a branch protection rule blocks force pushes, lift it temporarily. Open pull requests will lose their diffs, so merge or close them first.

Old copies still hold the secrets after the rewrite. Move your own working copy to the new history and remove the old objects.

Terminal window
git fetch origin
git reset --hard origin/main
git reflog expire --expire=now --all
git gc --prune=now

You cannot clean other people’s clones or forks. Ask everyone who has a copy to clone again.

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.

  • 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-removal option, so follow that procedure if you are starting fresh.