Sub topic 7
What to do when a push is blocked
This chapter assumes you know basic Git (commits, branches, git push, amending, and rebasing) and that push protection is already enabled on the repository. You should also know that a leaked credential is dangerous even in a private repository. The chapter covers what a block looks like, how to fix it correctly depending on where the secret sits in your local history, and when bypassing is the right call.
What a blocked push looks like
When push protection finds a supported secret in the commits you are pushing, the server rejects the whole push. No refs are updated, so nothing from that push lands on the remote. The error text is printed in your terminal and looks like this:
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: —— GitHub Personal Access Token ——————————————————————
remote: locations:
remote: - commit: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b
remote: path: config/deploy.py:12
remote:
remote: (bypass instructions and a URL follow)
To github.com:OWNER/REPO.git
! [remote rejected] main -> main (push declined due to repository rule violations)The details to read are the secret type, the commit SHA, and the file path with a line number. Those three tell you what leaked, where it entered your history, and which line to change. The exact wording can differ between releases, so treat the layout above as an example.
The key fact is that the secret is in your local commits only. It has not reached GitHub, so you can still keep it out of the shared history entirely. That is the best outcome, and the rest of the chapter is about reaching it.
The decision flow
Before you run any command, decide which situation you are in. Two questions matter: is the secret real, and is it in the latest commit or an earlier one?
The last branch matters. If a real credential has ever left your machine (pasted in chat, committed on another branch that was pushed, sent in a ticket), cleaning your local commits is not enough. Revoke it first. The chapter on remediation covers that sequence in detail.
Removing the secret from the latest commit
If the secret appears only in your most recent commit, remove it from the file and amend that commit. Amending replaces the commit with a new one, so the old commit that contains the secret is no longer reachable from your branch.
First, change the code to read the value from the environment:
import os
api_key = os.environ["PAYMENTS_API_KEY"]Then stage the file and amend:
git add config/deploy.py
git commit --amend --no-edit
git push origin main--no-edit keeps the existing commit message. The new commit has a different SHA, and the old one stays in your local reflog for a while but is not part of what you push. Push protection scans the commits being pushed, so the retry succeeds if no other secret remains.
Do not just add a new commit that deletes the secret. The push contains both commits, the earlier one still holds the value, and the push is blocked again. Git history is cumulative, so a later "remove secret" commit does not hide an earlier one.
Removing the secret from an earlier commit
If you committed the secret three commits ago and kept working, the amend trick does not reach it. Use an interactive rebase to edit the commit that introduced it. The block message gave you its SHA.
- Start the rebase from the commit before the bad one:
``bash git rebase -i 1a2b3c4d5e6f^ ``
- In the editor, change
picktoediton the line for that commit, then save and close. - Git stops at that commit. Fix the file, then run:
``bash git add config/deploy.py git commit --amend --no-edit git rebase --continue ``
- Later commits that touched the same lines may conflict. Resolve each conflict, run
git add, and continue. - Push again.
If the secret was added in one commit and removed in a later one, both inside the unpushed range, you can instead squash the two so the final result never contained it. Use fixup in the interactive rebase to fold the removal into the commit that added it.
Rewriting is safe here because the commits are unpushed. No one else has based work on them. If you have already pushed those commits to another branch or remote, stop. Rewriting would then diverge from shared history, and the secret may already be exposed.
You can check what is about to be pushed with:
git log --oneline origin/main..HEAD
git diff origin/main..HEADThe diff shows the net result, but the scan looks at each commit, so inspect individual commits too (git log -p origin/main..HEAD) when you search for the leftover value.
When the detected value is not a real secret
Push protection matches patterns, so it can flag values that look like credentials but are not. Common cases are example tokens in documentation, dummy values in test fixtures, and a revoked key kept for a unit test.
Prefer changing the value to something that does not match the pattern, such as ghp_EXAMPLE_TOKEN_REPLACE_ME. That avoids a bypass and avoids a stored alert. If the format must be realistic for a parser test, a bypass is reasonable.
If the same fixtures trigger blocks repeatedly, consider excluding that path from scanning. Be cautious: an exclusion also hides real secrets pasted into that path later. The chapter on path exclusions weighs that trade-off.
Bypassing the block
The block message includes a URL. Opening it lets you choose a bypass reason and, once you submit it, you can push the same commits again within a limited window. The reasons offered are:
| Reason | Use it when | Effect on the alert |
|---|---|---|
| It's used in tests | The value is a fixture or test credential | An alert is created and closed as used in tests |
| It's a false positive | The text only resembles a secret | An alert is created and closed as a false positive |
| I'll fix it later | The secret is real and you are deferring the cleanup | An alert is created and stays open |
Pick the reason that is true. The reason is recorded, and security managers can review bypasses. Choosing "I'll fix it later" for a real secret keeps the alert open, which is the honest state: the secret is now in the remote history and must be revoked.
In organizations where bypass is restricted, you may see a request flow instead of an instant bypass. You submit a request with a reason, and designated reviewers approve or deny it. The next chapter covers who can approve a bypass. Until a request is approved, the push stays blocked.
You can find bypassed pushes later through the API. The push_protection_bypassed field on each alert tells you whether a push-protection block was bypassed:
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}'Common mistakes
- Adding a deletion commit. The earlier commit still carries the secret and the push is blocked again.
- Amending the wrong commit. The block names a specific SHA. If you amend HEAD but the secret is two commits back, the same error returns.
- Fixing only one occurrence. The scan reports every detected secret, but some detections are reported together. After a fix, a new block may name a second secret. Search the diff for related values, such as a key and its matching secret.
- Fixing the file but not the history. Changing the working tree does nothing for commits already made. The history is what is scanned.
- Skipping revocation after a bypass. The credential is exposed, whatever reason you chose.
- Force-pushing over shared branches. Rewriting is for unpushed commits only.
- Thinking the block is only a warning. The push is rejected. Nothing is stored remotely, which is why it is worth handling carefully rather than reflexively bypassing.
Making the retry succeed
A short checklist for a clean second push:
- Read the secret type, SHA and path from the block message.
- Decide whether the value is real, and treat it as real if unsure.
- Remove the value from the code and read it from the environment or a secret manager.
- Fix history with
--amendfor the last commit or an interactive rebase for earlier ones. - Confirm with
git log -p origin/main..HEADthat the value no longer appears. - Push again.
- If the credential ever left your machine, revoke and rotate it now.
Exercise
You committed an API key in a file two commits ago, then made one more unrelated commit. git push is blocked and names the older commit. A teammate suggests adding a third commit that deletes the key. Why does this fail, and what should you do instead?
Show answer
The push contains every unpushed commit, and push protection scans each one. The older commit still holds the key, so the push is blocked again. Instead, run git rebase -i <older-commit>^, mark that commit as edit, remove the key from the file (reading it from the environment instead), run git add and git commit --amend --no-edit, then git rebase --continue. Resolve any conflicts and push again. Because the key never reached GitHub, no revocation is needed unless it was shared elsewhere.
Exercise
A block flags AKIAEXAMPLEFAKEKEY123 in test/fixtures/aws.json, a dummy value used by a parser test. Name two ways to proceed and the trade-off of each.
Show answer
Replace the value with something that does not match the AWS key pattern, which avoids a bypass and an alert but may weaken the test if the parser needs a realistic format. Or bypass with the reason "It's used in tests", which keeps the realistic value but creates an alert (closed as used in tests) and sets a precedent for bypassing. A path exclusion for test/fixtures/** is a third option, but it also hides real secrets added to that path later.