Sub topic 7
What to do when a push is blocked
This chapter assumes you know basic Git (commit, push, amend, rebase) and that push protection is already enabled on the repository. It covers what the block message tells you and how to respond to it.
What the block looks like
When you push a commit that contains a recognized secret, GitHub rejects the push. Nothing reaches the remote. The terminal output names the secret type, the commit, and the file path and line number. It also gives a URL where you can bypass the block, which the next chapter covers.
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: - GITHUB PUSH PROTECTION
remote: Push cannot contain secrets
remote: —— GitHub Personal Access Token ——
remote: locations:
remote: - commit: 1a2b3c4
remote: path: config/settings.py:12The default response is to remove the secret. A bypass is the exception.
Why the commit matters, not only the file
Push protection scans every commit in the push. If you delete the secret from the file and make a new commit, the earlier commit still contains it, so the push is blocked again. You have to rewrite the history so that no commit in the push contains the secret.
The right fix depends on where the secret appeared.
| Where the secret is | Fix |
|---|---|
| Only in your latest commit | `git commit --amend` after removing it |
| In an earlier unpushed commit | Interactive rebase, then edit that commit |
| Across several commits | Interactive rebase, or reset and recommit |
Fixing the latest commit
- Replace the hard-coded value with a read from the environment.
- Stage the change and amend the commit.
- Push again.
import os
api_key = os.environ["PAYMENTS_API_KEY"]git add config/settings.py
git commit --amend --no-edit
git pushFixing an earlier commit
Use an interactive rebase from the commit before the one that introduced the secret. Replace 1a2b3c4 with the commit ID from the error message.
git rebase -i 1a2b3c4~1
# mark the commit as "edit", save, then remove the secret from the file
git add config/settings.py
git commit --amend --no-edit
git rebase --continue
git pushIf the branch was already pushed earlier without the secret, a rebase changes the history, so you may need git push --force-with-lease. Use that form instead of --force. It refuses to overwrite work you haven't seen.
Choosing between removing and bypassing
Bypass only when the value is not a live credential. Examples are a dummy token in a test fixture or a documented example value. For test data that will recur, add a path exclusion rather than bypassing every time.
Common mistakes
- Adding a "remove secret" commit on top and pushing again. The earlier commit still contains the secret.
- Rewriting history but leaving the secret in another branch that you also push.
- Bypassing with "used in tests" for a value that is actually live.
- Forgetting to revoke the credential because the push never succeeded.
Exercise
You committed a database password in commit A, then made commit B, and your push was blocked. You delete the password in a new commit C and push again. Why is the push blocked again, and what should you do?
Show answer
Push protection scans every commit in the push, and commit A still contains the password. Rewrite history with an interactive rebase (or reset and recommit) so no commit contains it, then push. Also revoke and rotate the password, because it was exposed in your local history.