Sub topic 6
Finding the secret and the commits that contain it
Before you rewrite history, you need a complete list of where the secret lives. If you miss a copy, it survives the rewrite and you may not notice. This chapter covers how to search every ref, how to find files and strings, and how to record what you found.
This chapter assumes you have already rotated the credential and have a fresh clone (the previous two chapters). It also assumes you can read git log output and know what a ref is: a branch, tag, or other name that points to a commit.
Why "search the current files" is not enough
A search of your working tree only shows the checked-out commit. The secret may exist only in:
- an older commit on
mainthat a later commit deleted - a feature branch that was never merged
- a tag
- a file that was renamed, so its old path no longer exists
- a commit message
A good search covers every ref and every commit, and it covers both content and paths. You do this in three passes: look at the repository's shape, search for the exact string, then search for the pattern the string follows.
Pass 1: look at the repository's shape
git filter-repo --analyze reads the whole history and writes reports. It does not change anything, so it is safe to run first.
git filter-repo --analyze
ls .git/filter-repo/analysis
less .git/filter-repo/analysis/path-all-sizes.txtThe most useful reports for this task:
| File | What it tells you |
|---|---|
| `path-all-sizes.txt` | Every path that ever existed, with its size across history |
| `path-deleted-sizes.txt` | Paths that no longer exist at `HEAD` but remain in history |
| `blob-shas-and-paths.txt` | Which blob (file content object) was stored at which path |
| `directories-all-sizes.txt` | The same size information grouped by directory |
path-deleted-sizes.txt is the one people overlook. A file removed in a later commit is the classic "I deleted it" case, and this report lists it. Look for names like .env, secrets.*, credentials*, *.pem, id_rsa, *.pfx, terraform.tfstate, and config/*.yml.
Analysis finds files by name and size. It cannot tell you that a key was pasted into README.md or a test fixture. For that you need content searches.
Pass 2: search for the exact string
If you know the secret, use the pickaxe option, -S. It lists commits where the number of occurrences of a string changed. That means it finds both the commit that added the secret and the commit that removed it.
git log --all --oneline -S'AKIAEXAMPLEKEY12345'Add --source to see which ref each commit was reached from, and --name-status to see which files were touched:
git log --all --oneline --source --name-status -S'AKIAEXAMPLEKEY12345'Read the output as pairs. If you see two commits, the older one probably introduced the secret and the newer one removed it. Both belong on your list, because the secret is present in every commit between them, and in every commit after the first one if nobody removed it.
Two limits matter here:
-Smatches a literal string. A secret split across lines, base64-encoded, or URL-encoded will not match the plain form.-Sreports commits where the count changed. A commit that merely carries the secret along unchanged does not appear. That is fine for locating the origin, but it is why the verification chapter uses a different check.
Keep the secret out of your own traces
You are typing a live or recently live credential into a shell. Your shell history file keeps it. Since you rotated first, the risk is smaller, but the habit is worth having. Read the string into a variable instead of putting it on the command line:
read -r -s SECRET
git log --all --oneline --source -S"$SECRET"
unset SECRET-s stops the terminal echoing what you type, and the value never appears in your history file.
Pass 3: search for the pattern
The leak you know about may not be the only one. People who paste one key into a config file often paste others. Many credentials follow a recognizable shape, and -G searches added and removed lines with a regular expression (extended syntax by default).
git log --all --oneline --source -G'AKIA[0-9A-Z]{16}'
git log --all --oneline --source -G'-----BEGIN [A-Z ]*PRIVATE KEY-----'
git log --all --oneline --source -i -G'(password|secret|token|api[_-]?key)[[:space:]]*[=:]'The first pattern matches AWS access key IDs. The second matches PEM-encoded private keys. The third is deliberately broad: it will produce false positives such as password = os.environ[...], so review its hits by eye.
-S and -G differ in a way that affects your results:
| Option | Matches | Fires when |
|---|---|---|
| `-S'text'` | A literal string (or regex with `--pickaxe-regex`) | The number of occurrences in a file changes |
| `-G'regex'` | A regular expression | A changed line matches |
Use -S when you have the exact value. Use -G for shapes and families of secrets.
Searching file contents at every commit
The log options search diffs. To see the secret's actual surrounding text in each revision, use git grep against every commit:
git rev-list --all | xargs -n 50 git grep -I -n -E 'AKIA[0-9A-Z]{16}'git rev-list --all prints every commit reachable from any ref. xargs -n 50 hands them to git grep in batches of 50, which avoids "argument list too long" errors on large repositories. -I skips binary files, and -n prints line numbers. Each hit looks like <commit>:<path>:<line>:<text>. The command exits with a nonzero status when nothing matches, so a run with no output is a clean result.
This is slow on large histories, because it checks out each tree in memory. That cost is acceptable once, and you will run it again after cleanup.
Binary files need separate attention. -I skips them, so a key inside a keystore, a .pfx, or a compressed archive will not appear. Pass 1 is how you find those, by name and size.
Tracing a path through renames and deletions
When a file is the problem, find every commit that touched it, including after it was deleted or renamed:
git log --all --oneline --follow --name-status -- config/secrets.env
git log --all --oneline --diff-filter=D --name-only -- '*.env'--follow tracks renames but only works on a single file path. --diff-filter=D lists commits that deleted matching files, which finds files absent at HEAD. If a file was renamed, note every name it had. --invert-paths in the next chapter needs each path, because the tool matches paths as they appear in each commit.
You can also list the blobs directly:
git rev-list --all --objects | grep -E '\.(env|pem|pfx)$'Deciding which refs are affected
Once you have commits, find which branches and tags contain them:
git branch -a --contains <commit>
git tag --contains <commit>Replace <commit> with a hash from your results. A secret on one abandoned branch still counts, and the rewrite must reach that branch too. Note that --all also covers refs/stash and remote-tracking refs in your clone. A normal clone does not fetch pull request refs such as refs/pull/*, so your local search cannot see them. The hosting chapter deals with those.
Write the findings down
Save your results outside the repository, so that a later rewrite cannot erase them:
git log --all --format='%H %D %s' -G'AKIA[0-9A-Z]{16}' > ../affected-commits.txt
git log --all --name-only --format= -G'AKIA[0-9A-Z]{16}' | sort -u > ../affected-paths.txtThe first file is your before-and-after record. After the rewrite, these hashes will no longer exist, which is itself a useful check. The second file tells you whether to use path removal or text replacement:
- If the whole file is secret, such as
config/secrets.env, remove the path. - If a secret appears inside otherwise useful files, such as source code or documentation, replace the text.
- If both occur, do both, in separate runs or one run with both options.
Exercise
A teammate says, "I ran git log --all -S'AKIAEXAMPLEKEY12345' and got two commits, so the secret is only in those two commits." What is wrong with that conclusion, and what would you run to check it?
Show answer
-S reports commits where the number of occurrences of the string changed. It shows where the secret was added and removed, not every commit that contains it. Every commit between the two, on every branch descending from the first, also contains the secret. To see all commits and files that hold it, run git rev-list --all | xargs -n 50 git grep -I -n -F 'AKIAEXAMPLEKEY12345', and use git branch -a --contains <first-commit> to find the refs. Also check with -G and a pattern in case other secrets of the same shape exist, and use --analyze for binary files that git grep -I skips.
Exercise
You find the secret in config/secrets.env and also in a line of docs/setup.md. Which cleanup approach does each location call for?
Show answer
config/secrets.env is entirely secret, so remove the whole path with --invert-paths --path config/secrets.env. docs/setup.md is a useful file with one bad line, so use --replace-text to rewrite the secret string and keep the file.