Keeping API keys and Git secrets safe: a guide
One leaked API key in a public commit can be enough for someone to reach your account, and the first step is to revoke it at the provider. This page gathers the Git secrets guides on this site in one place: what to do in the first minutes after you spot a leak, how to remove the secret from all Git history, and how to reduce the chance of a leak before it happens.
Order matters. If a key leaked right now, start with the first guide immediately: revoke at the provider before any step in Git, because anyone can clone a public repository at any time. Then clean the history, then move on to the prevention guides.
Each guide shows the commands it ran and their output as they appeared during the test. This page has no ads inside its content and no monetized links.
I found a leaked key in my repository, where do I start?
Start with "Leaked API key: revoke first, then scan history", then "Removing a committed secret from all Git history", then follow the rest of the chain for prevention.
Who are these guides for?
Anyone working with API keys in Git projects, alone or on a team; every step is written with the command and its output as they appeared during the test.
Read in this order
- Leaked API key: revoke first, then scan history
The order decides how much damage is done. Revoke the key at the provider and create a new one before touching Git, because anyone can copy… - Removing a committed secret from all Git history
git-filter-repo is the recommended tool for rewriting history: the git filter-branch manual says its use is not recommended and calls its… - Deleting .env in a new commit does not hide the key
Deleting a file in a new commit hides it from the latest snapshot only. Every earlier commit still holds the full content, and anyone who… - Git still tracks .env after adding it to .gitignore
You added .env to .gitignore, yet git status keeps listing it as modified. - Storing API keys in a Python project
Keep the key outside the source file and read it when the program starts. - What a 12-line pre-commit hook catches, and what it doesn't
A pre-commit hook is the cheapest place to stop a credential from leaving a laptop, and this one is 12 lines of POSIX shell with no…