Sub topic 1
Why deleting a secret in a new commit is not enough
A developer commits an API key, notices the mistake, deletes the file, and pushes. The key is gone from the working tree. It is still in the repository. This chapter explains why, so the later chapters on rewriting history make sense.
Prerequisites
This chapter assumes you can do the following:
- Clone a repository, commit, branch, and push.
- Read a commit hash and understand that it identifies one commit.
- Run commands in a terminal.
You do not need any history-rewriting experience yet.
History is a chain of snapshots
Git stores each commit as a snapshot of the project. Each commit records the hash of its parent. A new commit that deletes a file does not edit the earlier snapshot. It adds one more snapshot in which the file is absent.
The commit that added the secret therefore stays in the chain. Anyone who can reach that commit can read the secret.
See it happen
This short demo builds a repository, commits a fake key, deletes it, and then reads it back.
git init demo && cd demo
echo 'API_KEY=AKIAEXAMPLEKEY12345' > secrets.env
git add secrets.env && git commit -m "Add config"
git rm secrets.env && git commit -m "Remove secrets"
git log --oneline -S'AKIAEXAMPLEKEY12345'
git show HEAD~1:secrets.envThe git log -S command lists commits where the number of occurrences of the string changed. It shows both the commit that added the key and the commit that removed it. The git show command prints the file as it existed one commit back. The key is fully readable.
Who can still read it
Deleting the file in a new commit does not change what these people and systems already hold:
- Anyone who cloned or fetched the repository. Their local copy contains every commit.
- Forks. A fork copies the full history at the time it was made.
- Other branches and tags. A branch that predates the deletion still points at the old snapshot.
- Hosting-platform caches and pull request refs. Many hosts keep extra references to commits.
- Automated scrapers. Bots scan public repositories for credentials and often find a key within minutes of a push.
The last point matters most. Assume that a pushed secret has been copied. You cannot know who read it, and you cannot make them forget it.
What this means for your response
Because you cannot recall copies, there are two separate jobs:
- Make the credential useless. Revoke it and issue a new one. This is the only step that actually stops misuse, and the next chapter covers it.
- Remove the secret from history. This reduces future exposure and keeps the repository clean. It does not undo past exposure.
The rest of this tutorial covers the second job. It uses git filter-repo to rewrite every commit so the secret never appeared. Doing that safely takes more than one command, because the rewrite changes commit hashes and affects everyone who shares the repository.
Exercise
You commit a password in commit A, then delete it in commit B and push both. A teammate says the problem is solved because the file is no longer in the latest version. What is wrong with that claim, and what command would show it?
Show answer
Commit A still contains the password, and anyone with the repository can read it. The latest version is only one snapshot in the chain. Running git log --all --oneline -S'the-password' lists commits A and B, and git show A:path/to/file prints the password. The credential also has to be revoked, because clones and forks may already hold a copy.