Sub topic 7
Removing a whole file from all history
This chapter assumes you can clone, commit, and push in Git, and that you know commits are identified by hashes. It also assumes you have already rotated the leaked credential and installed git-filter-repo in a fresh clone, as covered in earlier chapters.
When to remove the whole file
Sometimes the file exists only to hold the secret. A config/secrets.env file or a private key is a typical case. If nothing in the file needs to stay, delete it from every commit instead of editing its contents.
If the file has other useful content, such as a config file with one leaked password, replace the text instead. That approach is covered in the next chapter.
The command
Run this inside your fresh clone:
git filter-repo --invert-paths --path config/secrets.envTwo options work together here:
--path config/secrets.envselects the file.--invert-pathsflips the selection. Without it, filter-repo keeps only the selected path and drops everything else. With it, filter-repo drops the selected path and keeps everything else.
Leaving out --invert-paths is a common and destructive mistake. Your history would shrink to that single file. Check the result before you push anything.
What happens to the history
Every commit that contained the file is rewritten without it. Because a commit's hash covers its contents and its parent, every later commit also gets a new hash. Commits that become empty are dropped. Branches and tags are updated to point at the new commits.
Removing several paths or a pattern
You can repeat --path to remove more than one file:
git filter-repo --invert-paths --path config/secrets.env --path keys/deploy.pemFor a pattern, use --path-glob:
git filter-repo --invert-paths --path-glob '*.pem'Also list any earlier names of the file. If it was renamed at some point, filter-repo matches the path as written in each commit and does not follow renames. Use the path analysis from the earlier chapter, or git log --all --name-only, to find the older names.
Check the outcome
After the run, confirm two things.
- The file is gone from every commit:
git log --all --oneline -- config/secrets.envEmpty output means no commit on any ref touches that path.
- Everything else survived. Compare
git log --oneline | wc -lwith the count you noted before the run. It should be the same, apart from any commits that became empty.
The next chapters cover searching for the secret text itself and force-pushing the result. Note that filter-repo removes your origin remote after it finishes, which prevents an accidental push.
Exercise
You run git filter-repo --path config/secrets.env and the repository now contains only that one file. What did you leave out, and what does it change?
Show answer
You left out --invert-paths. Without it, --path tells filter-repo to keep only the listed path. Adding --invert-paths makes it remove the listed path and keep everything else. Re-clone and run the command again with both options.
Exercise
A file was renamed from .env to config/secrets.env last year. Why might removing only config/secrets.env leave the secret in history, and how do you check?
Show answer
Older commits store the file under .env, and filter-repo matches the path as written in each commit. Those commits keep the secret. Find the old names with git log --all --name-only and add --path .env to the command. Then run git log --all --oneline -- .env to confirm nothing remains.