Redact Git Repositories
So you fucked up big time and committed some sensitive personal information into a git repo? And, God forbid, you perhaps even pushed it to a remote? Not to worry, we've all been there :)
First and foremost, if the sensitive personal information was in the form of passwords, tokens, or private keys STOP READING THIS AND CHANGE/REVOKE THEM NOW. If it took the form of dox… well hopefully you're a nice person who doesn't make enemies easily.
Secondly, rethink if censoring your repo's history is the right thing to do. Keep in mind, if you're collaborating with people on this codebase, forcing them to rebase the repo's entire history and royally pissing all over their local work and branches will probably ruin their day. So, if you just leaked a password or PGP key, the answer is an easy NO. If you leaked your private journal, the answer is an easy YES and to HELL with the consequences.
Install the git-filter-repo tool and check git-filter-repo(1) and gitmailmap(5) for documentation on how to write the redaction rules. If you have any uncommitted changes, commit them; stashes and uncommitted changes will be lost. Then running the following command will edit every commit message and file committed since the beginning of time per the rules in ~/redactions.txt, will change committer names and email addresses per the rules in ~/mailmap.txt, and will delete any files matching the patterns in ~/hitlist.txt.
Before running this, use the --dry-run
flag and verify it's doing
what you want.
Aside: this works even when commits are signed, but I'm not sure what'll happen if you run it against a repository with signed commits made by other people.
git filter-repo --mailmap ~/mailmap.txt --replace-messages ~/redactions.txt --replace-text ~/redactions.txt --invert-paths --paths-from-file ~/hitlist.txt
You may also need to specify --force
.
Finally, force push each of your repo's branches to each of its remotes (if you have one named origin it'll have been removed, so add it back in manually) and apologize profusely to anyone you're collaborating with :)