Sub topic 2
Push Protection: Blocking Secrets Before They Land
Assume you know Git commits, branches, pushes, and basic repository settings. A secret is a credential such as an API key or access token. Secret scanning finds exposed credentials in repository content. Push protection checks commits before GitHub accepts a push and blocks a detected secret from reaching the repository.
What to Do When a Push Is Blocked
Treat the value as exposed, even though GitHub rejected the push. First identify whether it is a real credential. If it is real, remove it from the commit and revoke or rotate it with the provider. Then push the corrected commit.
If the secret exists only in an earlier local commit, edit or rebuild the local history before pushing. Removing the value from the newest file alone may leave it in earlier commits.
# Inspect the blocked commit, remove the credential, then amend the commit
git show --stat HEAD
git add path/to/file
git commit --amend
git push origin your-branchBypass Reasons and Approval
A bypass is appropriate only when the detection is a false positive, the value is a harmless test secret, or an approved exception applies. Select the reason GitHub requests and record why the value is safe. Repository or organization settings can require approval, so the person who pushes may not be able to authorize the bypass alone. Use the smallest exception and review bypasses regularly.
Working Through Secret Scanning Alerts
After a bypass or a secret committed through another path, secret scanning creates an alert. Triage each alert by checking the secret type, location, owner, and validity. Prioritize credentials reported as active. For a real leak, revoke or rotate the credential first, then remove it from current files and clean history if necessary. Close the alert only with an accurate resolution, such as revoked or false positive.
Exercise
A push is blocked because it contains an active cloud token. What should happen before cleaning Git history?
Show answer
Revoke or rotate the token first, then remove it from files and history. This prevents someone from using the credential during cleanup.