Leaked Secret in Git and How to Clean It Up the Right Way
You pushed a commit, looked at the diff a minute later, and saw an API key sitting in plain text in a config file. Your first instinct is to delete the line, commit again, and hope nobody noticed.

You pushed a commit, looked at the diff a minute later, and saw an API key sitting in plain text in a config file. Your first instinct is to delete the line, commit again, and hope nobody noticed. A leaked secret in Git does not work that way, and the order of the next few steps matters far more than how clever your cleanup command is.
This is the sequence I give small teams when it happens, whether the secret is a cloud access key, a database password, an SMTP credential or a payment API token.
Why a leaked secret in Git is already public
Deleting the line in a new commit removes it from the latest version of the file. It does not remove it from history. Anyone who runs git log with the patch view, anyone who cloned before your fix, and any CI runner that cached the repository still has the old commit. If the repository is public, automated scanners watch new pushes around the clock. Researchers who have planted test keys on purpose report that some get tried within five minutes of the push.
Private repositories buy less time than you think. Every clone, fork, backup and build log is a copy you do not control. Treat the secret as known to an attacker from the moment it left your machine.
The first 30 minutes: revoke before you rewrite
The single most important step is to make the leaked value useless. History rewriting comes later, and for some teams it never needs to happen at all.
- Revoke or rotate the credential at the provider. Log in to the service that issued it and disable the key, then create a new one. For a database password, change it on the server and restart the apps that connect. Aim to finish this within 15 minutes of noticing.
- Deploy the new value through your secret store or environment variables, not by committing it again. If your apps read from a .env file on the server, update that file by hand and restart.
- Check the provider's access logs for the window between the push and the revocation. Look for unfamiliar IP addresses, new resources, unusual sending volume, or calls from regions where nobody on your team works.
If you already have a habit of short lived keys, this step is boring, which is the point. Our piece on rotating API keys in small engineering teams covers how to set that up so a rotation takes minutes instead of an afternoon of hunting through services.
What to check in the logs
Cloud keys are the expensive ones. A leaked compute key can launch dozens of large instances for crypto mining, and bills of several thousand dollars in a weekend are common. For email credentials, check the outgoing queue and bounce rate; a sudden spike of 20,000 messages tells you the key was used for spam and your sending reputation may need repair.
Do you need to rewrite history at all?
Here is the counter-take that saves teams a lot of pain: once a secret is revoked, rewriting history is cleanup, not security. A dead key in an old commit cannot hurt you. Many guides jump straight to force pushing a rewritten history, and on a busy repository that causes more damage than the leak did. Collaborators must reclone, open pull requests break, and commit links in tickets stop resolving.
Rewrite history when one of these is true:
- The secret cannot be rotated, such as a private signing key that customers already trust, or a password reused somewhere outside your control.
- The file contained personal data, not just a credential, and you have a legal duty to remove it.
- The repository is public and you want scanners and casual readers to stop flagging the old commit.
If none of those apply, revoke, add a note to the commit message of your fix, and move on. That is a perfectly defensible outcome.
How to remove the secret from history when you must
The modern tool for this job is git filter-repo, a separate Python script that the Git project itself recommends over the older filter-branch command. It handles 50,000 commits in under a minute on a normal laptop.
- Tell the team to stop pushing. Post in your chat channel with a clear start time and an estimated end time, usually 30 to 60 minutes.
- Make a fresh mirror clone of the repository into a new folder. Do not run the rewrite in your normal working copy.
- Create a replacements text file with one line per secret, using the format the tool documents, so the leaked value becomes a marker like REMOVED. Using text replacement instead of deleting the whole file keeps the rest of the config history intact.
- Run filter-repo with its replace-text option pointing at that file. If the secret lived in a file that should never have been tracked, such as a .env file, use the path option with invert mode to drop that file from every commit instead.
- Search the result with git log and a pickaxe search for a distinctive part of the key. Zero hits means the rewrite worked.
- Force push all branches and tags, then ask your host to purge cached views. Most hosts keep old commits reachable through pull request references, and only their support team can garbage collect those.
- Have every developer reclone. Rebasing an old clone onto rewritten history is error prone, and one stale push brings the secret right back.
| Situation | Rotate | Rewrite history | Typical time |
|---|---|---|---|
| Cloud or API key in a private repo | Yes, immediately | Optional | 15 minutes |
| Same key in a public repo | Yes, immediately | Recommended | 1 hour |
| Private signing key | Plan a reissue | Yes | Half a day |
| Customer data in a fixture file | Not applicable | Yes, plus host purge | Half a day |
Stopping the next leak before it happens
Most leaks come from the same three places: a .env file that was never ignored, a debug script with a hardcoded token, and test fixtures copied from production. Fix each one with something automatic, because reminders do not survive a deadline.
- Ignore secret files from day one. Add .env, *.pem, and local override configs to .gitignore in the project template, and commit a .env.example with dummy values instead.
- Run a secret scanner in a pre-commit hook. Tools such as gitleaks or detect-secrets check staged changes in well under a second, and they catch the common key formats from major cloud and payment providers.
- Turn on push protection at your Git host if it offers it. It blocks a push that contains a known key pattern before the commit ever lands on the server.
Scanners belong next to the other cheap automated checks a small team can run, which we cover in dependency security checks every small dev team can run. For more on keeping accounts and servers tidy, browse the full security section.
Frequently asked questions
Does deleting a file from Git remove it from history?
No. Deleting a file only creates a new commit where the file is absent. Every earlier commit still contains it, and anyone with a clone can read it. Removing it from history requires a rewrite tool such as git filter-repo and a force push.
How fast do bots find secrets pushed to public repositories?
Often within minutes. Public push events are streamed and scanned continuously, and planted test keys have been used in under five minutes. Assume a key in a public push is compromised before you even notice it.
Is making the repository private enough after a leak?
It is not. Switching a repository to private stops new visitors, but it does nothing about clones, forks, mirrors or scanners that already copied the commit. Revoking the credential is the only step that actually closes the hole.
More in Security
Security
DDoS Protection for a Small Server Without a Big Budget
The first time one of my game servers got knocked offline, I spent two hours convinced the host had a network fault.
Security
Locking Down Your Domain Registrar Account the Right Way
A founder wrote to me last spring because his company email had stopped arriving. Not bouncing, just silent.
Security
Employee Offboarding Checklist to Revoke Access on Day One
Someone on your team hands in their notice, and three weeks later you discover their SSH key still opens the production box.