Skip to main content

此版本的 GitHub Enterprise Server 将于以下日期停止服务 2026-03-17. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

从命令行使用推送保护

了解如果 secret scanning 在你的更改中检测到机密,如何从命令行解除对 GitHub 的推送阻止。

谁可以使用此功能?

具有写入访问权限的用户

解决被阻止的推送

要解决被阻止的推送,你必须从机密出现的所有提交中删除该机密。

删除分支上最新提交引入的机密

  1. 从代码中删除机密。
  2. 要提交更改,请运行 git commit --amend --all。 这会更新引入机密的原始提交,而不是创建新提交。
  3. 使用 git push 推送更改。

删除分支上先前提交引入的机密

  1. 检查尝试推送分支时显示的错误消息,其中列出了包含该机密的所有提交。

    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
    
  2. 接下来,运行 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
    
    
  3. 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.
  4. 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.
  5. In the editor, choose to edit the commit identified in step 3 by changing pick to edit 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
    
  6. 保存并关闭编辑器,以开始交互式变基。

  7. 从代码中删除机密。

  8. 使用 git add . 将你的更改添加到暂存区。

    注意

    完整命令为 git add .: * add. 之间有一个空格。

    • 空格后的句点是命令的一部分。
  9. 使用 git commit --amend 提交更改。

  10. 运行 git rebase --continue 以完成变基。

  11. 使用 git push 推送更改。

绕过推送保护

注意

如果未看到绕过阻止的选项,则应从提交中删除敏感信息,或提交“绕过特权”请求以便能够推送被阻止的敏感信息。 请参阅 请求绕过权限

  1. 使用执行推送的同一用户身份,访问 GitHub 在推送被阻止时返回的 URL****。 如果其他用户尝试访问此 URL,他们将收到 404 错误。

  2. 选择最能描述为何应该能够推送机密的选项。

    • 如果机密仅在测试中使用,并且不会构成任何威胁,请单击“它在测试中使用”。
    • 如果检测到的字符串不是机密,请单击“它是误报”。
    • 如果机密是真实的,但你打算稍后修复它,请单击“稍后修复”。
  3. 单击“允许我推送此机密”****。

  4. 在三个小时内在命令行上重新尝试推送。 如果三小时内未推送,则需要重复此过程。

请求绕过特权

  1. 使用执行推送的同一用户身份,访问 GitHub 在推送被阻止时返回的 URL****。 如果其他用户尝试访问此 URL,他们将收到 404 错误。

  2. 在“或请求绕过特权”下,添加注释。 例如,可以解释为什么你认为推送机密是安全的,或者提供有关绕过阻止的请求的上下文。

  3. 单击“提交请求”。

  4. 查看电子邮件通知以获取对请求的响应。 审查完你的请求后,你将收到一封电子邮件,通知你该决定。

    • 如果请求获得批准,则可以将包含机密的提交以及包含同一机密的任何未来提交推送到存储库。
    • 如果请求被拒绝,在再次推送之前,需要先从所有提交中删除该机密。 有关如何删除被阻止的机密的详细信息,请参阅解决被阻止的推送

延伸阅读

  •         [AUTOTITLE](/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-in-the-github-ui)