Sub topic 3
Revoke, rotate, then clean up history
Assume you know basic Git commands, repository settings, API keys, and terminal use. When a secret is exposed, treat it as compromised even if the alert says it is inactive.
Revoke, rotate, then clean up history
Revoke the exposed credential at its provider first. Revocation stops the old value from working. Then rotate it by creating a replacement and updating every application, deployment, and secret store that used the old value.
Only after revocation should you clean the value from Git history. Removing text from a commit does not invalidate a credential. History cleanup can also require force-pushing and coordinating with other contributors.
flowchart TD
A[Secret detected] --> B[Revoke old credential]
B --> C[Create and deploy replacement]
C --> D[Clean secret from history]
D --> E[Verify alerts and consumers]caption: Secret remediation order alt: A flow from detecting a secret to revoking it, replacing it, cleaning history, and verifying the result.
Validity checks: prioritize active secrets
Validity checks can tell you whether a supported token appears active. Use that result to prioritize investigation, not to dismiss inactive-looking alerts. A token may become valid later, and unsupported providers may not offer a check.
Custom patterns and path exclusions
Use a custom pattern when your organization issues tokens that GitHub does not recognize. For example, an internal token might use this format:
acme_[A-Za-z0-9]{32}Test the pattern against real examples without storing live credentials. Exclude paths only when matches are intentionally harmless, such as fixtures containing dummy values:
# .github/secret_scanning.yml
paths-ignore:
- "docs/**/*.md"
- "test/fixtures/**"Review exclusions regularly. A broad exclusion can hide a real leak.
Automate configuration and reporting
With administrative access, enable protection and list open alerts through gh:
gh api --method PATCH repos/OWNER/REPO \
-f 'security_and_analysis[secret_scanning][status]=enabled' \
-f 'security_and_analysis[secret_scanning_push_protection][status]=enabled'
gh api 'repos/OWNER/REPO/secret-scanning/alerts?state=open' \
--jq '.[] | {number, secret_type_display_name, validity, push_protection_bypassed}'Resolve an alert only after remediation. For example, alert 42 can use revoked after the provider confirms revocation:
gh api --method PATCH repos/OWNER/REPO/secret-scanning/alerts/42 \
-f state=resolved \
-f resolution=revokedKeep secrets out of code
Store credentials in a secrets manager or environment variable, and inject them during deployment:
import os
api_key = os.environ["PAYMENTS_API_KEY"]Require push protection, review bypasses, scan generated files, and teach contributors to use placeholders in tests and documentation.
Exercise
A valid API key appears in a public commit. What happens first?
Show answer
Revoke it at the provider, create and deploy a replacement, then clean the old value from Git history.