Sub topic 9
Cleaning secrets out of commit messages
This chapter assumes you have already rotated the credential, made a fresh clone, and run git filter-repo at least once with --replace-text. If not, go through those chapters first.
Why `--replace-text` misses messages
Developers often paste a secret into a commit message. A typical case is a message like Fix deploy, key is AKIAEXAMPLEKEY12345. The --replace-text option only rewrites file contents (blobs). It never reads commit messages, so a history can look clean in every file and still show the secret in git log.
The option for messages is --replace-message. It rewrites commit messages and reads the same expressions file format as --replace-text.
Find the messages first
The -S search from the earlier chapter looks at changes in file contents, so it does not find messages. Use --grep instead:
git log --all --oneline --grep='AKIAEXAMPLEKEY12345'Each line in the output is a commit whose message contains the string. An empty result means no message needs cleaning.
The expressions file
You can reuse the file you wrote for --replace-text. Each line maps a match to a replacement, using ==> as the separator.
literal:AKIAEXAMPLEKEY12345==>REDACTED
regex:password\s*=\s*\S+==>password = REDACTEDA few rules apply:
- A line with no prefix is treated as a literal string.
- A line with no
==>replaces the match with***REMOVED***. regex:patterns use Python regular expression syntax.- Keep the file outside the repository. Otherwise the secret ends up in the history you are trying to clean.
Run the rewrite
Run it in your fresh clone:
git filter-repo --replace-message ../expressions.txtTo clean files and messages in one pass, pass both options. One pass is better than two. Each run rewrites every commit, and a second run needs --force because the clone is no longer fresh.
git filter-repo --replace-text ../expressions.txt --replace-message ../expressions.txtCheck the result
Search messages again, and confirm the replacement text appears where you expect:
git log --all --oneline --grep='AKIAEXAMPLEKEY12345'
git log --all --oneline --grep='REDACTED'The first command should print nothing. The second should list the commits you found earlier, now with the new text in their messages.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Running only `--replace-text` | Messages still contain the secret | Add `--replace-message` |
| Keeping the expressions file inside the repo | The secret is committed again | Store it one directory up |
| Running filter-repo twice in the same clone | The tool refuses without `--force` | Combine both options in one run |
| Searching messages with `-S` | Finds nothing, giving false confidence | Use `--grep` |
Exercise
A teammate says git filter-repo --replace-text ../expressions.txt finished successfully, but git log --all --grep='AKIAEXAMPLEKEY12345' still prints two commits. What happened, and how do you fix it in a fresh clone?
Show answer
--replace-text rewrites only file contents, so the two commits have the secret in their messages. In a fresh clone, run git filter-repo --replace-text ../expressions.txt --replace-message ../expressions.txt to clean both in one pass. Then repeat the --grep search to confirm it prints nothing.