How it works
A secret committed to a Git repository becomes part of an immutable object. Git never edits a commit: a later commit that removes the line only creates a new tree, and the branch moves to it. The earlier commit, and the secret in it, stays reachable. This is the core of hardcoded credential risk on GitHub, and it has three variants.
Secret in the current tree. A token, key or password sits in a file on the default branch: a .env file, a configuration file, a test fixture, a notebook. Anyone who can read the repository can read it, and for a public repository that is everyone, including the scanners that watch public pushes.
Secret in history after removal. The developer notices, deletes the line and pushes a fix. The value is still in the earlier commit, on other branches, in tags, and in the read-only pull request references under refs/pull/. GitHub also serves a commit by its hash for as long as the object exists on its side, even when no branch contains it any more. A force push that rewrites the branch therefore does not remove the old commit from GitHub by itself.
Secret in a fork that outlives the upstream. Forks of a repository share one object network. GitHub documents what happens when that network changes:
- Deleting a public repository promotes one of its public forks to be the new upstream. The forks stay.
- Making a public repository private leaves its public forks public, in a separate network.
- Deleting a private repository deletes its private forks.
- Making a private repository public leaves its private forks private, in separate private networks.
Truffle Security's research calls the consequence Cross Fork Object Reference: a commit pushed to any fork can be requested through another repository of the same network by its hash, including commits from a fork or upstream that was deleted, and commits made to a private fork before the upstream was made public. Short hashes work, and commit hashes can be learned from GitHub's public events API and from third-party archives of those events.
The author timestamp of a commit says neither when the secret first became public nor who read it. Each repository in a fork network has its own visibility timeline, and a deletion record for one repository is no inventory of the clones, mirrors and screenshots held elsewhere.
Prerequisites
- For public repositories and forks: nothing. No account is needed to clone or scan them.
- For private and internal repositories: read access, as a member, a collaborator, an app installation, or through a stolen token or SSH key.
- A secret that is still valid. The find only pays off when the credential was never rotated. Long-lived keys with no expiry are the usual case; see long-lived secret risk.
- For the fork variant: a commit hash, or the patience to enumerate hashes, and a surviving repository in the same fork network.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Choose the repositories
Start from the list of an organization's public repositories and forks, and the personal accounts of its members, which organization reconnaissance produces. Developers often push work code to personal repositories, which sit outside the organization's controls.
Code search finds obvious cases in current files:
# Private key headers and environment files in an organization's repositories
gh search code "BEGIN RSA PRIVATE KEY" --owner example-org --limit 100
gh search code password --owner example-org --filename .env
gh search code uses GitHub's legacy code search engine and has no regular expression search, so it is a first pass only.
2. Scan the full history
Clone everything, not only the default branch, then search every ref:
# All refs of the repository
git clone --mirror https://github.com/example-org/example-repo.git
cd example-repo.git
# Commits on any ref that added or removed a GitHub classic token prefix
git log --all -p -S 'ghp_'
Secret scanners do this for hundreds of credential formats at once. Gitleaks scans the output of git log -p:
# Scan the repository in the current directory, all refs, and write a report
gitleaks git -v --log-opts="--all" --report-path gitleaks-report.json
TruffleHog also tests each candidate against the issuer and can keep only the credentials that still work:
# One repository, verified secrets only, JSON output
trufflehog git https://github.com/example-org/example-repo --results=verified --json
# Every repository of an organization
trufflehog github --org=example-org --results=verified
# One repository with its issue and pull request comments
trufflehog github --repo=https://github.com/example-org/example-repo --issue-comments --pr-comments
Secrets are not only in code. Issue bodies, pull request descriptions, review comments, wikis and Actions logs carry pasted tokens too.
3. Recover deleted and dangling commits
Commits that no branch contains can still be requested by hash:
https://github.com/example-org/example-repo/commit/<commit-sha>
The public events timeline lists recent pushes of an organization (up to 300 events from the past 30 days):
gh api orgs/example-org/events --paginate --jq '.[] | select(.type == "PushEvent") | [.created_at, .repo.name, .payload.before, .payload.head] | @tsv'
A before value that no branch contains any more is the sign of a force push, and often of a secret someone tried to remove. TruffleHog has an experimental mode that enumerates such hidden commits of a repository and scans them:
trufflehog github-experimental --repo https://github.com/example-org/example-repo.git --object-discovery
4. Check the fork network
List the forks of the target, including forks that became the new upstream after a deletion, and scan each one. A fork can hold a branch the upstream never had.
gh repo list example-org --fork --limit 1000 --json nameWithOwner,parent
5. Use the credential
Identify the credential type from its format and test it against its issuer. GitHub documents a prefix for each of its token classes:
| Prefix | Credential |
|---|---|
ghp_ |
Personal access token (classic) |
github_pat_ |
Fine-grained personal access token |
gho_ |
OAuth access token |
ghu_ |
User access token of a GitHub App |
ghs_ |
Installation access token of a GitHub App |
ghr_ |
Refresh token of a GitHub App user access token |
The prefix classifies a find. It says nothing about validity, scope or use.
Impact
The attacker holds whatever the secret unlocks: a cloud account key, a database password, a signing key, a package registry token, or a GitHub token that opens the organization's private repositories. This is credential theft with no contact with the victim's systems until the credential is used, and the use looks like the legitimate owner.
A leaked GitHub token is often the start of a larger chain: clone the private repositories, harvest more secrets from them and from Actions configuration, then move into the cloud accounts those secrets belong to.
OPSEC and artifacts
- Anonymous cloning. Reading a public repository needs no account, and coverage of authenticated Git events can't establish anything about anonymous readers. There is no record of who cloned a public repository.
- Offline scanning. History scanning happens on the attacker's machine after one clone. For a private repository the clone itself is the only trace: one
git.cloneGit event per repository, kept for seven days and never shown in the web interface. - Verification is the noisy step. TruffleHog's verified results come from a live request to the issuer with the candidate credential. That request lands in the issuer's logs (a cloud audit trail, a GitHub audit log entry with the token's hash) from the scanner's address. A careful operator verifies from infrastructure that fits the victim, or doesn't verify at all.
- Public scanners race the attacker. GitHub revokes its own tokens when a valid one is pushed to a public repository or gist, and reports partner secrets in public repositories to the provider. A secret type without a provider pattern, or one in a private repository, gets no such protection.
- Cleanup points at the secret. A history rewrite changes every later commit hash, and clones that still hold the old history show exactly where the divergence is. A force push seen in the events timeline tells an observer which old commit to fetch.
Detection
GitHub can't tell you who read a leaked secret in a public repository. Detection is about finding the exposure before someone else does, and then finding use of the credential at its issuer.
- Secret scanning alerts. Secret scanning covers the entire Git history on all branches, plus issues, pull requests, discussions and wikis. It is free for public repositories; private and internal repositories need GitHub Secret Protection. Keep the repository and alert number together, because alert numbers are per repository.
# Open alerts whose credential the provider still reports as active
gh api -X GET orgs/example-org/secret-scanning/alerts -f state=open -f validity=active --paginate \
--jq '.[] | [.repository.full_name, .number, .secret_type, .validity, .publicly_leaked, .push_protection_bypassed] | @tsv'
# Where one alert's secret was found
gh api repos/example-org/example-repo/secret-scanning/alerts/1/locations \
--jq '.[] | [.type, .details.path, .details.commit_sha] | @tsv'
- Push protection bypasses. A contributor who is blocked and pushes anyway writes
secret_scanning_push_protection.bypass. Review each one: a blocked push alone doesn't put a secret into history, a bypassed one does. - Protections turned off. The secret scanning and push protection disabled rules watch events such as
repository_secret_scanning.disableandrepository_secret_scanning_push_protection.disable. - Visibility changes. A repository made public (
repo.access) publishes its whole history at once. Scan before the change, not after. - Collection from private repositories. The bulk clone rules on
git.clonecatch a token that copies many repositories to scan them offline. - Use of the credential. For a leaked GitHub token, hash it and search the audit log for
hashed_token:"VALUE". For other credentials, the issuer's own log is the only place use appears.
Order the events before drawing conclusions: the first commit that contained the secret, the time the repository or fork became readable to the audience in question, the alert creation time, and the issuer's revocation time. Publication can precede detection by weeks.
Mitigation
- Rotate first. Revoke or rotate the credential at its issuer as soon as the leak is known. Once the value can't be used, the copies in history, forks and clones are harmless. See secret rotation.
- Then clean the history, if it is still worth it. GitHub recommends
git-filter-repo(2.47 or later), followed by a forced mirror push:
git-filter-repo --sensitive-data-removal --invert-paths --path PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
git push --force --mirror origin
After the push, GitHub Support has to dereference or delete the affected pull requests, run garbage collection and remove cached views. Fork owners must clean or delete their forks, and every collaborator must discard old clones, or one git push brings the data back.
- Turn on secret scanning and push protection for every repository, and restrict who may bypass a block with delegated bypass. Push protection stops the commit before it reaches the repository, which is the only point where removal is cheap.
- Stop using long-lived secrets. Use OIDC federation from GitHub Actions to cloud providers, short-lived GitHub App installation tokens in place of personal access tokens, and a secret manager for what remains. The personal access token security guide covers the GitHub side.
- Control forking and visibility. Disable forking of private repositories where it isn't needed, and scan the full history and all forks before a private repository is made public.
- Include personal accounts in the search. Scan the public repositories of organization members for the organization's secret formats, since those repositories are outside organization policy.
Detection rules for this attack
Log events to collect
FAQ
Is deleting the file or the commit enough to remove a leaked secret?
No. A later commit only changes what the branch points to. The earlier commit stays in the history, in every clone and fork, in pull request references, and reachable on GitHub by its hash. The only reliable fix is to revoke or rotate the credential at its issuer. Rewriting history comes after that, and only reduces how easily the old value is found.
Can a secret survive the deletion of the whole repository?
Yes. When a public repository is deleted, one of its public forks becomes the new upstream of the network, and commits of the deleted repository can still be read through the remaining forks. Clones, mirrors and archives held by other people are outside GitHub's control altogether.
Does an open secret scanning alert mean the credential still works?
Not necessarily. The alert state is a triage status. An open alert can describe a credential that was already revoked, and a resolved alert records a decision, not a rotation. The validity field (active, inactive or unknown) is the provider-backed check, and the issuer's own record is the final answer.
Does GitHub revoke leaked tokens automatically?
GitHub revokes its own tokens (personal access tokens, OAuth tokens and GitHub App tokens) when a valid one is pushed to a public repository or public gist. For partner secret types found in public repositories it notifies the provider, who decides what to do. Secrets in private repositories and secret types without a pattern get no such treatment.
References
- Unsecured Credentials: Credentials In Files, T1552.001 (MITRE ATT&CK)
- Removing sensitive data from a repository (GitHub Docs)
- What happens to forks when a repository is deleted or changes visibility (GitHub Docs)
- About secret scanning (GitHub Docs)
- REST API endpoints for secret scanning (GitHub Docs)
- Anyone can access deleted and private repository data on GitHub (Truffle Security)
- TruffleHog (Truffle Security)
- Gitleaks
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.