解决被阻止的推送
要解决被阻止的推送,你必须从机密出现的所有提交中删除该机密。
- 如果机密是由你的最新提交引入的,请参阅删除分支上最新提交引入的机密。
- 如果机密出现在较早的提交中,请参阅删除分支上较早提交引入的机密。
删除分支上最新提交引入的机密
- 从代码中删除机密。
- 要提交更改,请运行
git commit --amend --all。 这会更新引入机密的原始提交,而不是创建新提交。 - 使用
git push推送更改。
删除分支上先前提交引入的机密
-
检查尝试推送分支时显示的错误消息,其中列出了包含该机密的所有提交。
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 -
接下来,运行
git log以查看分支上所有提交的完整历史记录及其对应的时间戳。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 logto identify which commit comes earliest in your Git history.- In the example, commit
8728dbe67was 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
picktoediton the first line of the text.edit 8728dbe67 my second commit message pick 03d69e5d3 my third commit message pick 8053f7b27 my fourth commit message -
保存并关闭编辑器,以开始交互式变基。
-
从代码中删除机密。
-
使用
git add .将你的更改添加到暂存区。注意
完整命令为
git add .: *add和.之间有一个空格。- 空格后的句点是命令的一部分。
-
使用
git commit --amend提交更改。 -
运行
git rebase --continue以完成变基。 -
使用
git push推送更改。
绕过推送保护
注意
如果未看到绕过阻止的选项,则应从提交中删除敏感信息,或提交“绕过特权”请求以便能够推送被阻止的敏感信息。 请参阅 请求绕过权限。
-
使用执行推送的同一用户身份,访问 GitHub 在推送被阻止时返回的 URL****。 如果其他用户尝试访问此 URL,他们将收到
404错误。 -
选择最能描述为何应该能够推送机密的选项。
-
如果机密仅在测试中使用,并且不会构成任何威胁,请单击“它在测试中使用”。
-
如果检测到的字符串不是机密,请单击“它是误报”。
-
如果机密是真实的,但你打算稍后修复它,请单击“稍后修复”。
注意
如果存储库启用了秘密扫描,则需要指定绕过推送保护的原因。
当推送到未启用机密扫描的_公共_存储库时,由于_用户的推送保护_(默认情况下,用户帐户处于启用状态),仍然可以防止意外推送机密。
通过用户的推送保护,如果公共存储库的推送包含受支持的机密,GitHub 将自动阻止这些推送,但无需指定允许该机密的原因,并且 GitHub 也不会生成警报。 有关详细信息,请参阅“用户的推送保护”。
-
-
单击“允许我推送此机密”****。
-
在三个小时内在命令行上重新尝试推送。 如果三小时内未推送,则需要重复此过程。
请求绕过特权
-
使用执行推送的同一用户身份,访问 GitHub 在推送被阻止时返回的 URL****。 如果其他用户尝试访问此 URL,他们将收到
404错误。 -
在“或请求绕过特权”下,添加注释。 例如,可以解释为什么你认为推送机密是安全的,或者提供有关绕过阻止的请求的上下文。
-
单击“提交请求”。
-
查看电子邮件通知以获取对请求的响应。 审查完你的请求后,你将收到一封电子邮件,通知你该决定。
- 如果请求获得批准,则可以将包含机密的提交以及包含同一机密的任何未来提交推送到存储库。
- 如果请求被拒绝,在再次推送之前,需要先从所有提交中删除该机密。 有关如何删除被阻止的机密的详细信息,请参阅解决被阻止的推送。
延伸阅读
-
[AUTOTITLE](/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-in-the-github-ui) -
[AUTOTITLE](/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-from-the-rest-api)