Sub topic 6
Push protection: blocking secrets before they land
This chapter assumes you can commit, push, and branch in Git. It also assumes you know what an API key or token is, and that you can open repository settings on GitHub. Earlier chapters covered how secret scanning finds credentials after they are committed, and how to turn the features on. Here you will see how push protection moves the check to the moment of the push.
What push protection does
Secret scanning is detective. It scans content that is already in the repository and raises an alert. By then the secret is in the Git history, and anyone with read access, or any clone or fork, may already have a copy.
Push protection is preventive. When you run git push, GitHub scans the commits you are sending. If it finds a supported secret, it rejects the push. The secret never reaches the remote repository, so no alert is needed and nothing has to be revoked yet.
The two features share detection patterns but act at different times.
| Feature | When it runs | What it does on a match | Secret reaches the remote? |
|---|---|---|---|
| Secret scanning | After content is in the repository | Creates an alert | Yes |
| Push protection | During `git push` | Blocks the push | No, unless the pusher bypasses |
They complement each other. Push protection cannot catch everything, because it covers only supported patterns and a user can bypass it. Secret scanning remains the safety net for those cases and for content that was committed before push protection was turned on.
What gets scanned
Push protection looks at the content of the commits in the push, including every commit in the range being sent, not just the latest one. A secret added in one commit and deleted in the next is still in the history, so it still blocks the push.
By default it checks for high-confidence patterns. These are provider token formats that are distinctive enough to produce few false positives, such as a cloud access key or a personal access token with a fixed prefix. Generic patterns, like a password in a config file, are far less certain, so they are not part of the default set. Which patterns apply depends on your plan and on settings such as custom patterns, which a later chapter covers.
Some limits are worth knowing:
- Only supported patterns are detected. A secret in an unusual format will pass.
- A push that contains no new commits for that content is not rescanned. Existing secrets in older history are the job of secret scanning alerts.
- Excluded paths (covered in a later chapter) are skipped.
Where it applies
Push protection works for pushes from several places, and it behaves a little differently in each.
- Command line.
git pushprints an error that lists the secret type, the commit, and the file path and line. This is the most common case. - Web interface. If you edit or create a file in the browser and the content contains a secret, GitHub blocks the commit and shows a message in the editor.
- REST API. Requests that create content, such as the contents API, can be blocked and return an error.
- GitHub Desktop and IDE integrations. These use Git underneath, so you see the same remote rejection as on the command line.
Push protection is enabled per repository, or for many repositories at once through an organization's security configuration. Users can also have push protection turned on for their own pushes to public repositories, which is a separate setting on a user account.
A blocked push, step by step
Suppose you committed a test script that contains a hard-coded key and ran git push. The output looks like this (details vary by secret type):
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote:
remote: - GITHUB PUSH PROTECTION
remote: —————————————————————————————————————————
remote: Resolve the following violations before pushing again
remote:
remote: - Push cannot contain secrets
remote:
remote: (?) Learn how to resolve a blocked push
remote: https://docs.github.com/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-from-the-command-line#resolving-a-blocked-push
remote:
remote: —— GitHub Personal Access Token ——————————————————————
remote: locations:
remote: - commit: 1f3c9ab2e8d4
remote: path: scripts/deploy_test.py:12
remote:
remote: (?) To push, remove secret from commit(s) or follow this
remote: URL to allow the secret.
remote: https://github.com/OWNER/REPO/security/secret-scanning/unblock-secret/XXXX
To github.com:OWNER/REPO.git
! [remote rejected] main -> main (push declined due to repository rule violations)Read three things in the message: the secret type, the commit hash, and the path:line location. The URL at the end is the bypass link, which you should treat as a last resort.
Note that the key is often in an older commit, not the tip. If you only edit the file in a new commit, the push is still rejected, because the earlier commit with the secret is still part of what you are sending.
Removing the secret before pushing
The correct response is usually to remove the secret and rewrite the unpushed commits so it never enters the remote history. Which technique to use depends on where the secret sits.
If it is in the most recent commit, edit the file and amend:
# replace the hard-coded value with an environment lookup first, then:
git add scripts/deploy_test.py
git commit --amend --no-edit
git pushIf it is in an earlier commit that has not been pushed, use an interactive rebase. Find the commit, mark it edit, fix the file, and continue:
git rebase -i origin/main
# change "pick" to "edit" for the offending commit, save, then:
git add scripts/deploy_test.py
git commit --amend --no-edit
git rebase --continue
git pushHere origin/main limits the rebase to commits you have not pushed. This is safe because rewriting local, unpushed history does not affect anyone else.
Because the secret was never accepted by the remote, you usually do not need to revoke it. If the value is a real credential, though, it has been on your disk and possibly in your shell history, so rotating it is cheap and sensible. If you have any doubt, rotate.
import os
api_key = os.environ["PAYMENTS_API_KEY"]Bypassing a block
When the finding is not a real secret, or the owner has already accepted the risk, you can bypass the block. Opening the unblock URL asks you to pick a reason:
- It's used in tests. The value is a dummy or fixture.
- It's a false positive. The matched text is not a credential.
- I'll fix it later. The secret is real and you are accepting the risk for now.
After you choose, you have a limited time to push again, and the push is accepted. GitHub then creates a secret scanning alert for the secret and records that push protection was bypassed, along with your reason. Your choice of reason shapes how reviewers triage that alert, so choose the accurate one.
Organizations can restrict who is allowed to bypass. With delegated bypass, contributors must submit a request, and designated reviewers approve or deny it. A later chapter covers those controls in detail.
Common errors and gotchas
- Editing only the tip commit. The push stays blocked because an earlier commit still contains the secret. Rewrite the commit that introduced it.
- Pushing a copy of the secret to another branch. Push protection scans every push, so the same secret is blocked again on any branch.
- Assuming a block means the secret is safe. The block protects the remote, but the value may exist in local files, logs, or CI output. Rotate real credentials.
- Bypassing for convenience. Each bypass creates an alert and teaches the team that the block is optional. Prefer moving the value to a secret store.
- Expecting full coverage. Unsupported formats pass through, so keep secret scanning and custom patterns in place.
- Confusing a block with a bypass alert. A blocked push creates no alert. Only a bypass produces one, and it carries the bypass reason.
Checking bypasses with the CLI
Because bypasses create alerts, you can audit them through the REST API. The push_protection_bypassed field tells you which alerts came from a bypassed push:
gh api 'repos/OWNER/REPO/secret-scanning/alerts?state=open' \
--jq '.[] | select(.push_protection_bypassed == true) | {number, secret_type_display_name, push_protection_bypassed_by: .push_protection_bypassed_by.login}'This prints one object per bypassed alert, including who bypassed the block. It is a quick way to review whether people bypass for good reasons.
Exercise
You run git push and receive a push protection error. The output points to a secret in commit a1b2c3d, which is two commits behind your latest local commit. You fix the file in a new commit and push again. Why is the push still blocked, and what should you do instead?
Show answer
Push protection scans every commit in the push, not only the tip. Commit a1b2c3d still contains the secret, so the push is rejected even though a later commit removes it. Rewrite the unpushed history so the secret never appears, for example with git rebase -i origin/main, marking a1b2c3d as edit, fixing the file, running git commit --amend --no-edit, and then git rebase --continue before pushing again. Rotate the credential if it was real.
Exercise
A teammate bypasses push protection with the reason "I'll fix it later" and the push goes through. What is created as a result, and what should be done with it?
Show answer
GitHub creates a secret scanning alert for the secret, marked as push protection bypassed, with the reason recorded. Because the secret is real and now in the remote history, the credential should be revoked at its provider, a new one issued and deployed, and only then should the alert be closed with the resolution "revoked".