API key exposed on GitHub: rotate it first, then remove it and check the damage
If an API key is exposed on GitHub, revoke or rotate it first. That is the most important step, and for a secret it comes before any cleanup. Removing the key from the code, pushing a new commit or recreating the repository does not stop anyone from using it. After the key is dead, check what it did, remove it from the history if you need to, and turn on push protection so the next one is blocked before it lands.
You pushed a key. The repository is public, or it was for a while, and your stomach dropped. Replace the key, look at the damage, then clean up. Doing the cleanup first feels productive and protects nothing. For the general first-hour list, see leaked API key: what to do.
01What happens when an API key is exposed on GitHub
Scanners do not wait. Automated scanners can locate publicly exposed secrets in minutes, and malicious actors can exploit them within hours. Bad actors continuously scan public repositories for keys, and even private repositories can be exposed through development environments.
So the safe assumption is that someone has the key. Consider any leaked secret immediately compromised. If you are not sure what the key can do, read what an API key is.
02Step 1: revoke or rotate the key
The action happens at the provider, not at GitHub. Log in to the provider's portal and find the section where the secret is managed. Revoke the secret, or rotate it. Rotating means you create a replacement, point your app at it, then revoke the old one. When downtime is a worry, generate the new secret with the same permissions first and revoke the old one after the switch. If the key is already being abused, revoke first and accept that the app stops until the new key is in.
If the exposed item is a GitHub token, GitHub may have done this step for you: a valid token pushed to a public repository or gist is automatically revoked. A revoked token cannot be restored, so you create a new one. If you think your account or your hardware may be compromised, you can revoke all your credentials at once from your account settings.
The provider pages list the details: OpenAI key leaked and AWS access key leaked.
03Step 2: check what the key did
Ask whether any logs or audit trails show the secret being used. The provider is the most reliable source on whether a secret is still valid. Look at usage and billing there, for the dates after the push.
On the GitHub side, git log -S searches the history to identify who committed the secret. That helps with the follow-up, not with the emergency.
04What GitHub's alert tells you
If secret scanning is on, an API key exposed on GitHub produces an alert in the Security and quality tab. One alert is created per secret per file, even if the same secret appears several times in that file. Validity checks show whether a detected secret is still active, which helps you decide what to fix first. Generic alerts, such as a private key or a password, can have a higher rate of false positives or secrets used in tests.
Two cases stay silent. A secret from a partner provider is reported to the provider, who can revoke the credential, and it is not displayed in your repository alerts. And when a resource needs paired credentials, an alert is created only if both parts are in the same file. No alert is therefore not the same as no leak.
05Step 3: take the key out of the code
Now fix the repository. Move the key into a .env file, a Keychain item or a secrets manager, and read it from the environment. The where to store guide shows each option.
If you committed the .env file itself, adding it to .gitignore does not help yet, because files already tracked by Git are not affected by it. The where to store guide shows how to stop tracking the file. A new commit hides the key from the current files only. The key stays in the older commits.
06Step 4: remove it from the Git history, if you need to
You do not always need this step. For a secret, revoking or rotating is the first step and may be enough on its own. Rewrite history only if you want the key gone from the repository for good, or if the data was not a secret you could revoke.
Rewriting has costs. Commit hashes change from the first affected commit on, there is a high risk of recontamination from old clones, and forks keep the commit. After the push, you must take a few more steps to fully remove the data from GitHub. The commands and the whole walk-through are on remove secrets from Git history.
07How to hide a key from GitHub next time
You cannot hide a key inside a public repository: anything committed there can be scanned. Hiding means never committing it. Keep the key in the environment of your app, loaded from a file that Git ignores or from a secrets manager. A private repository is no safe place either, because even private repositories can be exposed through development environments.
08Stop the next one: secret scanning and push protection
Two GitHub features do different jobs. Turn them on before the next key lands in a repository.
Secret scanning looks for keys already in the repository. It scans your entire Git history on all branches for hardcoded credentials, including API keys, passwords and tokens. On public repositories it runs automatically for free. Private repositories need GitHub Secret Protection switched on. For organization-owned repositories that requires GitHub Team or GitHub Enterprise Cloud. For an ordinary personal private repository GitHub does not list this feature: it is listed only for GitHub's enterprise plans. Where it is available, open the repository's Settings, click Advanced Security in the "Security and quality" section of the sidebar, then click Enable to the right of "Secret Protection". Alerts appear in the Security and quality tab of the repository.
Push protection works before the damage. It is off by default for a repository, and a repository administrator, organization owner or security manager can switch it on in that repository's security settings, next to Secret Protection; the exact clicks are on GitHub's push protection page. Rather than alerting you after the fact, it blocks pushes that contain secrets before they reach your repository. It explains why it blocked the push and asks you to remove the secret and push again.
| Feature | Default | Good to know |
|---|---|---|
| Push protection for a repository | Off by default | Anyone with write access can bypass it by giving a reason |
| Push protection for users | On by default for your account | Covers public repositories, and needs no reason or alert |
If you decide to bypass a block, open the URL GitHub gave you as the same user who pushed, because anyone else gets a 404 error. Then push again within three hours. Bypassed pushes are listed by filtering alerts with bypassed: true. Older versions of some tokens may not be covered by push protection. For a second layer on your own machine, see secret scanning and GitHub secret scanning explained.
09Safer tokens for GitHub itself
If the exposed key was a GitHub personal access token, treat it like a password. Fine-grained tokens are recommended over classic ones whenever possible. A classic token can access every repository you can access, while a fine-grained one can be limited to specific repositories.
10Where to go from here
An API key exposed on GitHub is fixed by the provider first and the repository second. Revoke or rotate, check usage, take the key out of the code, and rewrite history only if you must. The first-hour checklist covers every provider, and where to store API keys keeps the next key out of the repository.
FAQ
What happens if an API key is exposed?
Treat it as compromised right away. Automated scanners can find publicly exposed secrets in minutes, and malicious actors can exploit them within hours. With a secret key, an unauthorized party can make unauthorized charges, access customer data or disrupt your integration.
Is it safe to put a secret API key in a public GitHub repository?
No. Bad actors continuously scan public repositories for keys, and even private repositories can be exposed through development environments.
How can I hide my API key in GitHub?
Keep it out of the repository: put it in a .env file and list that file in .gitignore, or use a secrets manager. If it was already committed, ignoring the file is not enough, because files already tracked by Git are not affected by .gitignore.
What can someone do with my API key?
Whatever the key allows on your account, at your cost. A key is account credentials, like a username and password.
Does GitHub revoke an exposed key by itself?
For its own tokens, yes: a valid OAuth token, GitHub App token or personal access token pushed to a public repository or gist is automatically revoked. For other providers, GitHub notifies the provider, who can revoke the credential.
Is deleting the commit enough?
No. Removing the secret from the codebase, pushing a new commit, or deleting and recreating the repository does not prevent the secret from being exploited. Revoke the key with its provider.
Related guides
Sources
- GitHub docs, remediating a leaked secret
- GitHub docs, removing sensitive data from a repository
- GitHub docs, token expiration and revocation
- GitHub docs, managing personal access tokens
- GitHub docs, about secret scanning
- GitHub docs, enabling secret scanning for your repository
- GitHub docs, about push protection
- GitHub docs, push protection from the command line
- GitHub docs, about secret scanning alerts
- Git documentation, gitignore
- GitHub gitignore, Node template
- Stripe docs, API key best practices
- Artificial Immunity