About push protection
Push protection helps to prevent security leaks by scanning for secrets before you push changes to your repository.
When you try to push a secret to a repository secured by push protection, GitHub blocks the push. You must remove the secret from your branch before pushing again. For more information on how to resolve a blocked push, see "Resolving a blocked push on the command line" and "Resolving a blocked commit in the web UI" in this article.
If you believe it's safe to allow the secret, you have the option to bypass the protection. For more information, see "Working with push protection."
For information on the secrets and service providers supported for push protection, see "Secret scanning patterns."
Resolving a blocked push on the command line
When you attempt to push a supported secret to a repository secured by push protection, GitHub will block the push. You can remove the secret from your branch or follow a provided URL to allow the push.
Notes:
- If your Git configuration supports pushes to multiple branches, and not only to the current branch, your push may be blocked due to additional and unintended refs being pushed. For more information, see the
push.default
options in the Git documentation. - If secret scanning upon a push times out, GitHub will still scan your commits for secrets after the push.
Removing a secret introduced by the latest commit on your branch
If the blocked secret was introduced by the latest commit on your branch, you can follow the guidance below.
- Remove the secret from your code.
- To commit the changes, run
git commit --amend
. This updates the original commit that introduced the secret instead of creating a new commit. - Push your changes with
git push
.
Removing a secret introduced by an earlier commit on your branch
You can also remove the secret if the secret appears in an earlier commit in the Git history. To do so, you will need to identify which commit first introduced the secret and modify the commit history with an interactive rebase.
-
Examine the error message that displayed when you tried to push your branch, which lists all of the commits that contain the secret.
remote: —— GitHub Personal Access Token —————————————————————— remote: locations: remote: - commit: 8728dbe67 remote: path: README.md:4 remote: - commit: 03d69e5d3 remote: path: README.md:4 remote: - commit: 8053f7b27 remote: path: README.md:4
-
Next, run
git log
to see a full history of all the commits on your branch, along with their corresponding timestamps.test-repo (test-branch)]$ git log commit 8053f7b27 (HEAD -> main) Author: Octocat <1000+octocat@users.noreply.github.com Date: Tue Jan 30 13:03:37 2024 +0100 my fourth commit message commit 03d69e5d3 Author: Octocat <1000+octocat@users.noreply.github.com> Date: Tue Jan 30 13:02:59 2024 +0100 my third commit message commit 8728dbe67 Author: Octocat <1000+octocat@users.noreply.github.com Date: Tue Jan 30 13:01:36 2024 +0100 my second commit message commit 6057cbe51 Author: Octocat <1000+octocat@users.noreply.github.com Date: Tue Jan 30 12:58:24 2024 +0100 my first commit message
-
Focusing only on the commits that contain the secret, use the output of
git log
to identify which commit comes earliest in your Git history.- In the example, commit
8728dbe67
was the first commit to contain the secret.
- In the example, commit
-
Start an interactive rebase with
git rebase -i <COMMIT-ID>~1
.- For
<COMMIT-ID>
, use the commit identified in step 3. For example,git rebase -i 8728dbe67~1
.
- For
-
In the editor, choose to edit the commit identified in step 3 by changing
pick
toedit
on the first line of the text.edit 8728dbe67 my second commit message pick 03d69e5d3 my third commit message pick 8053f7b27 my fourth commit message
-
Save and close the editor to start the interactive rebase.
-
Remove the secret from your code.
-
Commit your changes using
git commit --amend
. -
Run
git rebase --continue
to finish the rebase. -
Push your changes with
git push
.
Resolving a blocked commit in the web UI
When you use the web UI to attempt to commit a supported secret to a repository secured by push protection, GitHub will block the commit.
You will see a dialog box with information about the secret's location, as well as options allowing you to push the secret. The secret will also be underlined in the file so you can easily find it.
To resolve a blocked commit in the web UI, you need to remove the secret from the file. Once you remove the secret, you will be able to commit your changes.
Alternatively, if you determine that it's safe to allow the secret, use the options displayed in the dialog box to bypass push protection. For more information about bypassing push protection from the web UI, see "Working with push protection."