Sub topic 8
Replacing secret strings across history
This chapter covers rewriting file contents when a secret sits inside a file you need to keep. It assumes you have already rotated the credential, you work in a fresh clone, and you know which commits contain the secret from the previous chapters. You also need permission to force-push later.
When to replace text instead of removing a file
--invert-paths deletes a whole file from every commit. That is the wrong tool when the file is still needed, such as a settings file with one hardcoded key. --replace-text rewrites the contents of every file in every commit and swaps each match for a replacement string. The file stays in history, but the secret does not.
Write an expressions file
--replace-text reads rules from a file. Each line has a match, then ==>, then the replacement. A line is a literal string by default. Prefix it with literal: to say so explicitly, or with regex: to use a regular expression.
literal:AKIAEXAMPLEKEY12345==>REDACTED
regex:password\s*=\s*\S+==>password = REDACTEDKeep this file outside the repository, for example in the parent directory. It contains the secret itself, so it must never be committed. If a line has no ==>, the matched text is replaced with the default ***REMOVED***.
Run the rewrite
Run the command inside the fresh clone:
git filter-repo --replace-text ../expressions.txtThe tool walks every commit on every ref. It rewrites each file blob that matches a rule and gives every affected commit a new hash. Commits that do not contain a match are rewritten too if their ancestors changed, because a commit's hash depends on its parents.
Choose literal or regex rules
Literal rules are safer. They match exactly the text you give, with no surprises. Use a regex when the secret varies, for example when several commits hold different passwords on lines with the same shape.
| Rule type | Good for | Risk |
|---|---|---|
| `literal:` | A known key or token | Misses any variant you did not list |
| `regex:` | Many values with one pattern | A loose pattern can alter unrelated code |
Test a regex on a scratch clone first and read the result with git diff against a backup clone. A pattern like password\s*=\s*\S+ also matches harmless lines such as password = None, and those would be rewritten too.
What to check afterwards
The tool prints a summary of the commits it parsed and rewrote. Confirm that the working tree still builds and that the redacted file still parses. A line like api_key = REDACTED is valid in most formats, but a JSON number replaced by a word is not, so pick replacements that keep the file valid.
Also remember that git filter-repo removes the origin remote after a run. That is a deliberate safety step so you do not push by accident. You will restore it when you are ready to force-push.
Exercise
Your repository has settings.py with a hardcoded token tok_live_8f3a91 in forty commits. You still need settings.py. Which option do you use, and what does the expressions file line look like?
Show answer
Use --replace-text, not --invert-paths, because the file must stay. The line is literal:tok_live_8f3a91==>REDACTED, stored outside the repository, and you run git filter-repo --replace-text ../expressions.txt in a fresh clone.
Exercise
Why can a broad regex rule be dangerous, and how do you reduce the risk?
Show answer
It can match and change harmless text in many commits, which breaks code or hides real changes. Prefer literal rules, and test any regex on a scratch clone and compare the result before you rewrite the real one.