Sub topic 1
Why deleting a secret in a new commit is not enough
A developer commits an API key, notices the mistake a minute later, deletes the line, and commits again. The file looks clean, so the problem seems fixed. It is not. This chapter explains why the key is still exposed, who can read it, and what that means for how you respond. Later chapters cover the cleanup itself.
Prerequisites
This chapter assumes you already have the following:
- Basic Git use: you can clone, commit, branch, and push.
- An understanding that commits are identified by hashes.
- Comfort with a command line and a text editor.
- A Git installation (any version from the last several years) if you want to run the demo below.
You do not need git filter-repo yet. Permission to force-push to a repository only matters in later chapters.
What a commit really stores
A Git commit is not a list of changes. It is a snapshot of the whole project at one moment. Each commit records:
- a pointer to a tree (the directory contents, including every file's blob),
- the author, the date, and the message,
- the hash of its parent commit (or parents, for a merge).
The commit's hash is computed from all of that content, including the parent hash. Change anything in an old commit and its hash changes. Because every commit includes its parent's hash, the hashes of all later commits change too.
This design is why history is trustworthy. It is also why a "fix" commit cannot erase anything. A new commit adds a new snapshot on top of the old ones. It does not edit them.
What happens when you delete a secret
Here is the situation in the first scenario, step by step:
- Commit A adds
.envcontaining the key. - Commit B deletes
.env.
Commit B's snapshot has no .env. Commit A's snapshot still has it, and commit A is still the parent of commit B. Anyone who can read the repository can check out A, or ask Git to print the file as it existed in A.
See it for yourself
The following script builds a small throwaway repository, commits a fake key, removes it, and then reads it back. The key is a made-up example string, not a real credential.
git init leak-demo
cd leak-demo
git config user.email "[email protected]"
git config user.name "Demo"
echo "AWS_ACCESS_KEY_ID=AKIAEXAMPLEKEY12345" > .env
git add .env
git commit -m "Add config"
git rm .env
git commit -m "Remove secret"
# The current files are clean.
git grep 'AKIAEXAMPLEKEY12345' || echo "not in the current files"
# History is not.
git show HEAD~1:.env
git log --all --oneline -S'AKIAEXAMPLEKEY12345'The output looks like this, with different hashes on your machine:
not in the current files
AWS_ACCESS_KEY_ID=AKIAEXAMPLEKEY12345
3f1c2ab Remove secret
9d0e7c4 Add configThree things are worth noticing:
git grepsearches only the files at the current commit, so it reports nothing.git show HEAD~1:.envreads the file as it was one commit ago, and prints the key.git log -Slists every commit where the number of occurrences of the string changed. Both the commit that added the key and the commit that removed it appear.
If your own project is affected, that git log -S command is the quickest way to learn how far back the exposure goes. A later chapter uses it to plan the cleanup.
Where the secret lives
The key is not only in the repository on the server. Each place below holds its own copy, and you control only some of them.
| Location | Why it has the secret | Who controls it |
|---|---|---|
| The hosting service | Every pushed commit is stored, including old ones | You, through the host's settings |
| Every clone | `git clone` copies the full history, not just the latest snapshot | Each collaborator |
| Forks | A fork copies the repository, history included | The fork's owner |
| Pull request refs | Hosts keep special refs for pull requests, so commits from PR branches stay reachable | The host |
| CI runners and build caches | Pipelines clone or cache the repository | Your CI administrator |
| Local backups and mirrors | Disk backups copy `.git` directories | The machine's owner |
| Package or container artifacts | A build may have copied the file into a published image or archive | Whoever published it |
A public repository adds a further problem. Automated scanners watch public pushes and event feeds for strings that look like cloud keys and tokens. Attackers have used leaked cloud keys within minutes of publication. You should assume that a secret pushed to a public repository was read, even if you removed it quickly.
A private repository is safer, but not safe. Contractors, former employees, compromised laptops, and misconfigured access settings all expose private history.
The reflog and unreachable commits
Two edge cases come up often.
First, the local reflog. Git records where branch tips used to point. If you run git reset --hard HEAD~1 to "undo" the commit with the secret, the commit disappears from the branch, but the reflog still references it. Git keeps unreachable objects for a while (by default, entries expire after 30 or 90 days, depending on the entry), and until a garbage collection removes them they can be recovered:
git reflog
git show HEAD@{1}:.envSecond, a force-push does not delete anything on its own. If you push a rewritten branch to the host, the old commits may still be fetchable by their full hash, and may remain visible through pull request refs or cached views. Hosts clean up unreferenced objects on their own schedule, and some require a support request. This is why a force-push is part of cleanup but never the whole of it.
Treat the secret as compromised
These facts lead to one rule: once a secret has been committed and pushed, treat it as compromised. This holds even if:
- the repository is private,
- you deleted the file within seconds,
- you rewrote history afterward,
- nobody has reported misuse.
You cannot prove that no one copied the secret. You can only make a copied secret useless, and that means revoking the credential at its source. Rotation (issuing a new credential and disabling the old one) is therefore the first response step. The next chapter covers it. Rewriting history comes after.
The order matters for a practical reason. Cleaning history takes time: you must find every occurrence, rewrite, verify, force-push, and ask others to re-clone. During that time the old key stays valid unless you revoke it first.
What rewriting history can and cannot do
Rewriting history is still worth doing. It removes the secret from the places you control, stops new clones from receiving it, and removes the easiest route for a casual reader. It also keeps scanners from flagging your repository again and again.
It does not do any of the following:
- It does not make the old credential safe. Only revocation does that.
- It does not change other people's clones, forks, or backups.
- It does not remove copies that the hosting service keeps.
- It does not remove the secret from places outside Git, such as chat messages, issue comments, build logs, and published artifacts.
Think of the cleanup as two separate goals. Rotation limits the damage from copies you cannot reach. Rewriting limits how many new copies appear.
Common mistakes
- Deleting the file in a new commit and calling it done. The history still holds it.
- Using
git reverton the commit that added the secret. A revert is itself a new commit, so the original stays in the history. - Searching only the current files. Use
git log --all -S'...'to search every ref, and check tags and other branches, not justmain. - Cleaning only the default branch. The secret may sit on a feature branch, a tag, or a branch someone else pushed.
- Rotating the key but not telling the owner of dependent systems. A new key must be deployed wherever the old one was used, or something breaks.
- Assuming a private repository means no exposure. Anyone with read access, now or earlier, could have copied it.
Summary
A commit is an immutable snapshot linked to its parent by hash, so a later commit that removes a file leaves the earlier snapshot intact. Anyone with a clone, a fork, or access to the host can read the secret from that earlier snapshot. The correct response is to treat the credential as compromised and revoke it. History rewriting then reduces the remaining exposure, as the following chapters show.
Exercise
You committed a database password, then pushed a second commit that removes the file. A teammate says the problem is solved because git grep finds nothing. Give two reasons the password is still exposed, and name one command that shows it.
Show answer
First, git grep searches only the files in the current commit, but the earlier commit that added the password is still in the history, and every clone, fork, and the host's copy contains it. Second, because a new commit does not edit its parent, anyone can check out the older commit or read the file from it. A command that shows it is git log --all --oneline -S'<password>', which lists the commits where the string was added or removed, or git show <commit>:<path>, which prints the file as it was in that commit. Because copies may already exist, the password must also be revoked and replaced.
Exercise
A colleague runs git reset --hard HEAD~1 to drop the commit that contains an API key, and does not push. Is the key gone from their machine? Explain.
Show answer
Not necessarily. The reset moves the branch back, but the commit and its blob remain in the object database and are listed in the reflog until Git expires the entry and garbage-collects the object. Running git show HEAD@{1}:<path> can still print the file. The key also remains valid at its source, so it must be revoked regardless.