On this page
Credentials end up in Git for ordinary reasons: a test script with a hard-coded key, a .env file added by git add ., a debug commit that was meant to be reverted. Once pushed, a secret lives in every clone and every fork, and deleting the file in a new commit does not remove it from history. Attackers know this and scan public commits continuously.
GitHub secret scanning detects known credential formats in repository content and history, and push protection blocks them before they are pushed. This guide covers enabling both across an organization, handling bypasses, custom patterns, and what to do when an alert fires.
What is available on which plan
- Public repositories: secret scanning alerts and push protection are free and on by default for supported patterns.
- Private and internal repositories: require GitHub Secret Protection, the standalone product that replaced the secret scanning part of GitHub Advanced Security in 2025. It adds push protection, custom patterns, validity checks, delegated bypass and AI-assisted detection of generic passwords.
- Personal accounts: every user can turn on push protection for yourself in account settings, which blocks supported secrets on pushes to any public repository.
Even without a Secret Protection license, enable everything you can on public repositories and ask every developer to turn on personal push protection.
Enable it across the organization
Use security configurations rather than per-repository toggles. Under Settings > Advanced Security > Configurations, create a configuration (or use the GitHub-recommended one) with:
- Secret scanning: Enabled
- Push protection: Enabled
- Validity checks: Enabled where available
- Non-provider patterns: Enabled where available
Apply it to all repositories and set it as the default for new repositories. Enforce it so repository admins cannot switch it off.
To check coverage with the API:
gh api --paginate "/orgs/ACME/repos" \
--jq '.[] | [.name,
.security_and_analysis.secret_scanning.status,
.security_and_analysis.secret_scanning_push_protection.status] | @tsv' |
awk '$2 != "enabled" || $3 != "enabled"'
Every line printed is a repository that is not covered.
How push protection behaves
When a developer pushes a commit containing a supported secret, the push is rejected with a message that names the secret type, file and line, and offers a URL to bypass. On the command line, the rejection appears as remote: lines under a GITHUB PUSH PROTECTION heading, followed by the commit, path and line of each detected secret.
The developer should remove the secret from the commit, not just from the latest file version:
# Secret is in the last commit only
git reset --soft HEAD~1
# remove the secret, move it to a secrets manager or environment variable
git add -p && git commit
# Secret is in an older unpushed commit
git rebase -i origin/main # edit the offending commit
Push protection also checks content uploaded through the web UI and, for supported patterns, content sent through the REST API, such as file creation.
Control bypasses
By default, a blocked user can bypass push protection by choosing a reason: It's used in tests, It's a false positive or I'll fix it later. Each bypass creates an alert (closed for the first two reasons, open for the third) and an audit log event.
For sensitive organizations, turn on delegated bypass in the security configuration. Bypass then becomes a request that a designated reviewer team approves or denies. The developer gets a link to the request; the reviewers see the secret type and location. Use it at least for repositories classified as production.
Monitor bypasses either way:
gh api --paginate "/orgs/ACME/audit-log?phrase=action:secret_scanning_push_protection.bypass" \
--jq '.[] | [.created_at, .actor, .repo] | @tsv'
Custom patterns
Built-in patterns cover hundreds of provider token formats. Your own credentials (internal API keys, database URLs with embedded passwords, signing keys with a known header) need custom patterns. Define them at the organization or enterprise level so that they apply everywhere:
- Secret format: a regular expression for the secret itself, for example
acme_live_[A-Za-z0-9]{32}. - Before secret and After secret: context, for example
\band\b, orpassword=to reduce false positives. - Must match / must not match: additional constraints, for example must contain a digit.
Run the dry run against selected repositories before publishing, and enable push protection for the pattern once the false positive rate is acceptable. A simple win is to give your internal tokens a unique prefix in the first place; prefixed formats are what make provider patterns precise.
Triage alerts
Secret scanning alerts list the secret type, the commits where it appears, and, with validity checks, whether the credential is still active. Prioritize active secrets, then secrets in public repositories, then everything else.
# Open alerts across the organization, newest first
gh api --paginate "/orgs/ACME/secret-scanning/alerts?state=open&sort=created&direction=desc" \
--jq '.[] | [.number, .repository.name, .secret_type_display_name, .validity, .html_url] | @tsv'
Closing an alert as revoked should mean exactly that. Closing as false positive or used in tests should be rare and reviewable.
Responding to a leaked secret
When a real secret is exposed, the order of operations matters:
- Revoke or rotate the credential first. Removing it from the repository does nothing for copies already cloned, forked or scraped. If the provider supports it, revoke the key and issue a new one.
- Check for use. Look at the provider's logs (for example AWS CloudTrail for an access key) for activity from unexpected IPs between the commit time and the revocation time.
- Replace the secret in the application with a reference to a secrets manager or, for CI, an OIDC-based short-lived credential.
- Clean history if needed. For private repositories, rewriting history is optional once the secret is revoked. If you do it, use
git filter-repoand coordinate a force push; then contact GitHub Support to purge cached views and pull request references. Forks keep their copy. - Close the alert as revoked, and record the incident.
GitHub also runs a secret scanning partner program: for public repositories, many providers receive matching tokens automatically and may revoke them. Do not rely on that for your response.
Keep secrets out of Actions logs
Secret scanning looks at repository content, not at workflow logs. Actions masks the values of registered secrets in logs, but derived values (a secret base64-encoded, or a JSON field extracted from a secret) are not masked automatically. Register them with ::add-mask:: before printing anything derived:
token=$(echo "$CREDS_JSON" | jq -r .token)
echo "::add-mask::$token"
The Actions security guide covers secret scoping with environments, and the script injection guide covers the workflows that most often leak secrets to attackers.
Prevent leaks earlier
Push protection is the last gate before GitHub. Earlier gates help too:
- A
.gitignorethat excludes.env,*.pem,*.p12and local credential files in every repository template. - A pre-commit hook running a scanner such as Gitleaks or TruffleHog on staged changes.
- A push ruleset that blocks file extensions like
.pemand.pfx(see rulesets and branch protection). - Fine-grained tokens with expiration, so a leaked token has a bounded lifetime.
References
- About secret scanning (GitHub Docs)
- About push protection (GitHub Docs)
- Supported secret scanning patterns (GitHub Docs)
- REST API endpoints for secret scanning (GitHub Docs)
- Removing sensitive data from a repository (GitHub Docs)
- git-filter-repo (project repository)
- Gitleaks (project repository)
- MITRE ATT&CK T1552.001: Unsecured Credentials in Files
- OWASP Secrets Management Cheat Sheet
Tags:repositoriessecrets
Written by
Florian Amette, Security Engineer & EditorSecurity engineer writing practical hardening guides for GitHub organizations, repositories and CI/CD pipelines, with a focus on software supply-chain security.