Sub topic 1
Why committed secrets are a problem
A secret is a value that proves identity or grants access: an API key, a database password, a cloud access key, a private key or an OAuth token. When one is committed to a Git repository, it stops being a private value. This chapter explains why, so the rest of the tutorial (secret scanning, push protection, alerts and remediation) has a clear purpose.
Prerequisites
This chapter assumes you already have the following:
- Basic Git usage: committing, pushing, branching and reading history with
git log. - A GitHub account and some familiarity with repository settings.
- A working idea of what API keys, tokens and passwords are used for.
- Comfort running commands in a terminal.
You do not need any prior knowledge of GitHub's security features.
What counts as a secret
A secret is any credential whose possession lets someone act as you or as your system. Typical examples:
- Cloud provider access keys, such as an AWS access key ID with its secret access key.
- Personal access tokens for GitHub or other services.
- Payment, email or messaging API keys.
- Database connection strings that contain a password.
- Private keys for TLS, SSH or code signing.
- Webhook signing secrets and session-signing keys.
Configuration values such as a hostname or a feature flag are not secrets. The test is simple: if an attacker could use the value to gain access, treat it as a secret.
Why a commit is not a private place
Developers often assume a secret is safe because the repository is private or because they will delete it soon. Several properties of Git and GitHub make that assumption unreliable.
History is permanent by design
Git stores every commit as an immutable snapshot. Deleting a file in a later commit adds a new snapshot without the file. The older snapshot still contains it. Anyone who can read the history can read the secret.
git init demo && cd demo
echo 'PAYMENTS_API_KEY=example-not-a-real-key' > .env
git add .env
git commit -m "Add config"
git rm .env
git commit -m "Remove config"
git log -p -- .envThe final command prints both commits. The diff of the first commit shows the PAYMENTS_API_KEY line in full, even though the file no longer exists in the working tree. Removing the line is a change to the latest state, not to the past.
Copies spread quickly
Once a commit is pushed, you no longer control every copy of it. Copies can exist in:
- Every clone made by teammates, build servers and CI runners.
- Forks. On GitHub, a fork of a public repository keeps the full history.
- Pull request refs and branches that still point at the old commit.
- Cached views, mirrors and archives.
- Local backups and developer laptops.
Rewriting history on the remote does not reach any of these copies. A leaked secret should therefore be treated as exposed even after a successful cleanup. The correct order of actions, revoke first and clean history second, is covered in a later chapter.
Private repositories are still a wide audience
A private repository limits who can read it, but not to people you fully trust forever. Consider who has access over time:
- Everyone with read access, including contractors and former employees whose access was never removed.
- Third-party apps and integrations that were granted repository access.
- Anyone who later gains access if the repository is made public, transferred or open-sourced.
- An attacker who compromises one developer account and gets read access to everything that account could see.
A secret in a private repository turns every one of those events into a credential leak.
Public repositories are scanned by attackers
For public repositories, the risk is immediate. Attackers run automated scanners that watch public activity, such as new pushes, and test strings that look like credentials. Exposed cloud keys are commonly abused within minutes of being pushed. You should not assume that a secret you exposed for "just a short while" went unnoticed.
What happens after a leak
The following diagram shows a typical sequence. The key point is that the time between the push and the revocation is the window in which the secret can be misused.
Deleting the file in the diagram changes nothing for the attacker. Only revocation at the service provider ends the window.
The consequences
The impact depends on what the credential can do. Common outcomes include:
- Unexpected cost. Stolen cloud keys are used to start compute instances, often for cryptocurrency mining, and the bill goes to the key owner.
- Data exposure. A database password or storage key can let an attacker download customer data, which can trigger legal and regulatory obligations.
- Lateral movement. A token with access to one system often reveals more credentials, such as those stored in a CI system or a secrets file.
- Supply chain risk. A leaked package registry token or signing key lets an attacker publish malicious versions of your software.
- Reputation and response cost. Even when no data is stolen, the incident costs time: investigation, rotation across services, and communication.
How secrets usually end up in commits
Most leaks are accidents, not negligence. Knowing the common paths helps you design defenses later in this tutorial.
| Cause | Example |
|---|---|
| Local config file added by mistake | A `.env` file picked up by `git add .` |
| Hard-coded test credential | A "temporary" key pasted into source to get a demo working |
| Debug output committed | A log file or a saved HTTP request containing an `Authorization` header |
| Infrastructure files | A Terraform state file or a Kubernetes manifest with a plaintext password |
| Copy and paste | A snippet from a chat or ticket that included a real token |
| Notebook output | A Jupyter notebook that saved a cell result showing an environment variable |
A single mistake is enough. Because every commit is permanent, a one-line slip remains in history until someone acts.
Common misconceptions
- "I deleted it in the next commit." The previous commit still has the value. Only revocation makes the value useless.
- "The repository is private, so it is fine." Private limits the audience but does not remove the exposure to current and future readers.
- "Nobody will find it." Automated scanners do the finding, and they do not need to know your repository exists.
- "I will rewrite history and the problem is gone." Rewriting helps tidy the remote, but clones, forks and caches may keep the old commits. Rewriting also does not undo any misuse that already happened.
- "Encoding hides it." Base64 or similar encoding is reversible and offers no protection. Attackers decode candidates routinely.
How GitHub addresses the problem
GitHub offers two complementary defenses, both explored in the following chapters.
- Secret scanning inspects repository content and history for strings that match known credential formats, and raises alerts so you can respond.
- Push protection checks a push before it lands, and blocks it when it contains a recognized secret, so the secret never enters the remote history.
Detection after the fact leaves a window of exposure, as the timeline showed. Prevention at push time narrows it. Neither replaces the habit of keeping secrets out of code, which is the subject of the final chapter.
The practical mindset to carry forward is this: a committed secret is a compromised secret. Your first question after a leak is not "how do I remove it from Git?" but "how do I make it worthless?"
Exercise
A teammate commits an API key to a private repository, notices a minute later, and pushes a second commit that deletes the file. They say the problem is solved. Give two reasons it is not, and name the action that actually ends the risk.
Show answer
First, the earlier commit still contains the key, so anyone with read access to the history (including clones and forks made before the deletion) can read it. Second, private repositories are read by many people and integrations over time, and any of them or any compromised account could have copied the key. The action that ends the risk is revoking the key at the service provider and issuing a replacement. Cleaning history is a later, secondary step.
Exercise
Run the demo commands from this chapter in a scratch directory, using a fake value. Which command shows that the "deleted" value is still recoverable, and what does its output include?
Show answer
git log -p -- .env shows it. The output lists both commits that touched .env. The diff for the first commit includes the line PAYMENTS_API_KEY=example-not-a-real-key, and the second commit shows that line removed. The value remains readable in history.