git 防止在 GitHub 上推送到 master?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46146491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:07:18  来源:igfitidea点击:

Prevent pushing to master on GitHub?

gitgithubbranchaccess-controlpull-request

提问by joshlf

GitHub allows you to configure your repository so that users can't force push to master, but is there a way to prevent pushing to master entirely? I'm hoping to make it so that the only way of adding to commits to master is through the GitHub pull request UI.

GitHub 允许您配置您的存储库,以便用户不能强制推送到 master,但是有没有办法完全阻止推送到 master ?我希望做到这一点,以便向 master 添加提交的唯一方法是通过 GitHub 拉取请求 UI。

采纳答案by Cory

Since the original question / answer, Github has added a new option for this to the restricted branches UI which allows you to set this up.

自从最初的问题/答案以来,Github 为此向受限分支 UI 添加了一个新选项,允许您进行设置。

Require pull request reviews before mergingWhen enabled, all commits must be made to a non-protected branch and submitted via a pull request with the required number of approving reviews and no changes requested before it can be merged into a branch that matches this rule.

合并前需要拉取请求启用后,所有提交都必须提交到不受保护的分支,并通过拉取请求提交,该拉取请求具有所需数量的批准,并且在合并到符合此规则的分支之前没有请求任何更改。

Edit: the answer below this one provides more details, including that this doesn't work if you have admin privliges.

编辑:此答案下方的答案提供了更多详细信息,包括如果您具有管理员权限,这将不起作用。

回答by Ignacio Arces

The current accepted answer is actually correct but if you are an organization owner or have admin privileges, which is the case if you created the repo, you can still push to protected branches. From Github documentation at https://help.github.com/en/articles/about-branch-restrictions:

当前接受的答案实际上是正确的,但是如果您是组织所有者或具有管理员权限(如果您创建了回购就是这种情况),您仍然可以推送到受保护的分支。来自https://help.github.com/en/articles/about-branch-restrictions 的Github 文档:

Organization owners and people with admin permissions for a repository are always able to push to a protected branch.

组织所有者和拥有仓库管理员权限的人始终能够推送到受保护的分支。

For any other type of collaborator, git pushwill fail.

对于任何其他类型的合作者,git push都会失败。

If you really want to disable pushing qt all, then you have to setup that locally either by configuring an invalid pushRemotefor a branch as mentioned before:

如果您真的想禁用全部推送 qt,那么您必须通过pushRemote为前面提到的分支配置无效来在本地进行设置:

git config branch.master.pushRemote no_push

git config branch.master.pushRemote no_push

or you can setup a pre-push hook as shown here: https://gist.github.com/vlucas/8009a5edadf8d0ff7430

或者你可以设置一个 pre-push hook,如下所示:https: //gist.github.com/vlucas/8009a5edadf8d0ff7430

回答by phd

You can enable branch restrictionsand decide who (in terms of users and teams of the organization) are allowed to push.

您可以启用分支限制并决定允许谁(根据组织的用户和团队)推送。

https://help.github.com/articles/about-branch-restrictions/

https://help.github.com/articles/about-branch-restrictions/

?Note: If the "Include administrators" is checked and you've enabled required status checks on the branch and they fail, any attempt to push changes to the base branch will also fail, regardless of a user or team's permission status.?

?注意:如果“包括管理员”被选中并且你已经在分支上启用了必需的状态检查并且它们失败了,任何将更改推送到基本分支的尝试也将失败,无论用户或团队的权限状态如何。?

回答by Tomoyuki Aota

Directly pushing to remote's master is rejected when status checksare enabled, meaning that the only way to add commits on remote's master is merging pull requests (which pass the status checks) on GitHub.

当启用状态检查时,直接推送到远程的主服务器会被拒绝,这意味着在远程主服务器上添加提交的唯一方法是在 GitHub 上合并拉取请求(通过状态检查)。

Here is my experiment result for the master branch requiring status checks:

这是我对需要状态检查的 master 分支的实验结果:

  1. Create a commit on the master branch on my PC.
  2. Push to remote's master.
  3. A rejection message appears. The commit is not pushed to remote in the end.
  1. 在我的 PC 上的 master 分支上创建一个提交。
  2. 推送到遥控器的主人。
  3. 出现拒绝消息。提交最终不会推送到远程。
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]> git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: error: GH006: Protected branch update failed for refs/heads/master.
remote: error: 3 of 3 required status checks are expected.
To https://github.com/TomoyukiAota/photo-location-map.git
 ! [remote rejected] master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/TomoyukiAota/photo-location-map.git'
C:\GitRepo\GitHub\TomoyukiAota\photo-location-map [master ↑1]>

回答by tal952

If You are using Node, You can use huskyto create a pre-push validation that a direct push to master does not happen. That way, You can still use your administrator privileges to merge PRs. I guess other languages have similar solutions to husky.

如果您使用的是 Node,您可以使用husky创建一个预推送验证,不会发生直接推送到 master 的情况。这样,您仍然可以使用您的管理员权限来合并 PR。我猜其他语言对 husky 有类似的解决方案。

  1. npm install husky --save-dev
  2. In /.huskyrc.js:
  1. npm install husky --save-dev
  2. /.huskyrc.js
const preventPushToMaster = `branch=\`git symbolic-ref HEAD\`
if [ "$branch" = "refs/heads/master" ]; then
    echo "\033[31mDirect push to master is not allowed.\033[0m"
    exit 1
fi`;

module.exports = {
  hooks: {
    'pre-push': preventPushToMaster,
  },
};