Sub topic 9
Cleaning secrets out of commit messages
This chapter assumes you have read the chapters on --replace-text and on verification. You should already be comfortable with an expressions file, a fresh clone, and the git log -S search. Here you extend the same workflow to text that --replace-text never touches: the messages attached to commits and tags.
Why file rewriting leaves messages behind
--replace-text rewrites the contents of files (blobs). A commit message is not a file. It is a field inside the commit object, so a run that scrubs every blob can still leave the secret in git log.
Secrets reach messages in a few common ways:
- A developer pastes a command into the message, such as
Fix deploy: aws configure set aws_access_key_id AKIA.... - A commit explains a rotation and quotes the old and new values.
- An annotated tag carries release notes that include a token.
- A message quotes a connection string copied from an error log.
Hosting platforms display messages prominently on commit pages, in pull request timelines, and in search. A secret in a message is easy to find and easy to scrape.
Why `-S` will not find it
git log -S'string' searches the diff of each commit for changes in the number of occurrences of a string. It does not look at messages. A search that returns nothing for -S tells you nothing about messages.
Use --grep, which matches against the message text:
git log --all --oneline --grep='AKIAEXAMPLEKEY12345'--grep is case-sensitive by default and treats its argument as a basic regular expression. Add -F to match a fixed string, which avoids surprises with characters such as . or *:
git log --all --oneline -F --grep='AKIAEXAMPLEKEY12345'Annotated tags have their own messages, and git log does not list them. Search them through the ref listing:
git for-each-ref --format='%(refname)%09%(contents)' refs/tags | grep -F 'AKIAEXAMPLEKEY12345'For lightweight tags, %(contents) shows the message of the commit they point to. That commit is already covered by git log --grep.
Using `--replace-message`
--replace-message takes the same kind of expressions file as --replace-text. Each line is a literal string or a regex: pattern, followed by ==> and the replacement. Because the format is identical, one file can serve both options.
literal:AKIAEXAMPLEKEY12345==>REDACTED
regex:password\s*=\s*\S+==>password = REDACTEDKeep this file outside the repository. If you put it inside, the secret ends up in a new commit or in your working tree, which defeats the purpose.
The rules for the file are the same as before:
| Detail | Behavior |
|---|---|
| No prefix | The expression is treated as literal text. |
| `regex:` prefix | The expression is a Python regular expression, matched against bytes. |
| No `==>` on a line | The match is replaced with `***REMOVED***`. |
| Backreferences | In a regex replacement, `\1` refers to the first capture group. |
The option applies to commit messages and to annotated tag messages. It does not change authors, committers, ref names, or file contents.
Running it
You can run --replace-message alone, as you did in the spine example, or combine it with --replace-text in a single pass:
git filter-repo --replace-text ../expressions.txt --replace-message ../expressions.txtOne pass is better than two. Each filter-repo run rewrites every affected commit and changes hashes, so running twice means two rounds of changed hashes. It is also easier to reason about a single before-and-after state.
A runnable demonstration
This script builds a throwaway repository with a secret in a message, then removes it. It uses --force because a repository created with git init is not a fresh clone, and filter-repo refuses to run on one without it. Never use --force this way on your real repository. For real work, start from a fresh clone, as covered earlier.
set -e
mkdir demo-messages && cd demo-messages
git init -q
git config user.name "Demo User"
git config user.email "[email protected]"
echo "hello" > notes.txt
git add notes.txt
git commit -q -m "Add notes" -m "Tested with key AKIAEXAMPLEKEY12345 on staging"
git tag -a v1.0 -m "Release 1.0, token AKIAEXAMPLEKEY12345"
echo "--- before ---"
git log --all --oneline -F --grep='AKIAEXAMPLEKEY12345'
printf 'literal:AKIAEXAMPLEKEY12345==>REDACTED\n' > ../expressions.txt
git filter-repo --replace-message ../expressions.txt --force
echo "--- after ---"
git log --all --oneline -F --grep='AKIAEXAMPLEKEY12345' || true
git log -1 --format=%B
git cat-file -p v1.0After the rewrite, the first git log prints nothing. The commit body reads Tested with key REDACTED on staging. The tag message reads Release 1.0, token REDACTED. The commit hash and the tag object hash have both changed.
What happens to hashes mentioned in messages
Messages often refer to other commits, as in Revert abc1234. Since a rewrite changes hashes, filter-repo updates abbreviated and full hashes in messages to point at the new commits when it can find them. This keeps references meaningful, but it also means messages change even when your expressions do not match them. That is expected behavior.
Where else a secret can hide
A secret can survive in places neither replace option reaches. The table lists them and the tool that fits each.
| Location | Rewritten by | How to find it |
|---|---|---|
| File contents | `--replace-text` | `git log --all -S'string'` |
| Whole file path | `--invert-paths --path` | `git log --all --name-only` |
| Commit message | `--replace-message` | `git log --all --grep` |
| Annotated tag message | `--replace-message` | `git for-each-ref` on `refs/tags` |
| Author or committer name or email | `--mailmap` or `--email-callback` | `git log --all --format='%an %ae %cn %ce'` |
| Branch or tag name | `--refname-callback` | `git for-each-ref` |
Author fields and ref names are rare places for a secret, but they do occur. A branch called fix-token-ghp_abc123 is a leak. Callbacks are outside this chapter's scope, but keep the table in mind when you verify.
Verifying messages are clean
Your verification step needs a message check in addition to the content check. The spine's -S and git grep commands cover file content only. Add these:
git log --all --oneline -F --grep='AKIAEXAMPLEKEY12345'
git for-each-ref --format='%(refname)%09%(contents)' | grep -F 'AKIAEXAMPLEKEY12345' || echo 'no matches'The second command lists every ref, including branches and remote-tracking refs, along with each ref's message contents. It catches annotated tag messages. Empty output from the first and the no matches message from the second mean no message contains the string.
If you used a regex: expression, search with the same pattern, not only the literal value. A regex that matches password = hunter2 will not be verified by searching for AKIA.... Use git log --all --grep with -E for extended regular expressions:
git log --all --oneline -E --grep='password\s*=\s*[^ ]+'Check that the pattern matches nothing except the replacement. The replacement password = REDACTED itself matches this search, so exclude it. A tighter pattern such as password\s*=\s*(\S*[^D\s]|\S*[^E]D) is awkward. Searching for the original secret values is usually simpler and more reliable.
Common errors
- Searching with
-Sand concluding the secret is gone.-Signores messages. Always add--grep. - Running
--replace-textand skipping--replace-message. The file contents are clean and the log still shows the key. - Expressions file inside the repository. The filter-repo run does not protect the file, and a later
git add .can commit it. Keep it in the parent directory. - Regex too broad.
regex:password.*==>Xcan swallow the rest of the line, including text you wanted to keep. Test the pattern withgit log --all --grepfirst. - Forgetting that hashes changed. Every commit after the first modified message gets a new hash. Collaborators must re-clone, as covered in the coordination chapter.
Summary of the workflow for messages
- Rotate the credential.
- In a fresh clone, find the secret in messages with
git log --all -F --grepand in tag messages withgit for-each-ref. - Add the secret to an expressions file stored outside the repository.
- Run
git filter-repo --replace-text ... --replace-message ...in one pass. - Verify with both content and message searches.
- Re-add the remote and force-push.
Exercise
You ran git filter-repo --replace-text ../expressions.txt on a fresh clone. git log --all -S'AKIAEXAMPLEKEY12345' returns nothing, but a teammate says the key still appears on the commit page of the hosting site. What is the likely cause, and what command finds and fixes it?
Show answer
The key is probably in a commit message. -S searches diffs only, and --replace-text rewrites only file contents. Find it with git log --all --oneline -F --grep='AKIAEXAMPLEKEY12345', then rewrite with git filter-repo --replace-message ../expressions.txt. Rewrite messages together with file contents in one run if you have not yet pushed.
Exercise
An expressions file has the single line regex:token=\S+, with no ==>. What does --replace-message do with a message containing token=abc123?
Show answer
A line with no ==> replaces each match with ***REMOVED***. The message text becomes ***REMOVED***, because the pattern matches the whole token=abc123 string.