Sub topic 1
Removing Leaked Secrets from Git History with git filter-repo
Prerequisites
You should know how to clone, commit, branch, and push with Git. You should also understand commit hashes, use a command line, and have permission to force-push the affected repository.
Why deleting a secret in a new commit is not enough
A Git commit records a complete snapshot of tracked files. If a secret appears in commit A, deleting it in commit B creates a later clean snapshot, but commit A still exists. Anyone who can access the repository can inspect its history, an old branch, or a clone.
Treat every committed secret as compromised. Rotate or revoke it before cleaning Git. History rewriting does not invalidate a cloud key, password, token, or certificate.
Rotate the credential first
Use the provider that issued the credential to revoke it or create a replacement. Update applications and deployment systems with the replacement. Then record the old value only in a secure incident record, not in the repository.
What rewriting history actually does
History rewriting creates new commits whose snapshots no longer contain the selected data. Because each commit hash depends on its contents and its parent, the rewritten commits receive new hashes. Branches and tags must then point to these new commits.
git filter-repo versus filter-branch
git filter-repo is the modern tool for repository-wide filtering. It is faster and safer by default for common cleanup tasks. git filter-branch is older, slower, and easier to misuse, so prefer git filter-repo for new work.
Installing git filter-repo and starting from a fresh clone
Run the tool in a disposable, fresh clone. This preserves your working copy and avoids carrying unexpected local refs into the rewrite.
pip install git-filter-repo
git clone [email protected]:org/repo.git repo-clean
cd repo-cleanThe clone is the input to the rewrite. Later chapters cover locating the secret, removing paths, replacing text, verifying every ref, and publishing the rewritten history.
Exercise
Why must you rotate a secret before rewriting Git history?
Show answer
Rewriting changes repository history but does not revoke a credential that may already have been copied.