git 如何保护github中的“master”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10381672/
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-10 13:34:52  来源:igfitidea点击:

How to protect "master" in github?

gitgithubbranchaclprotection

提问by yegor256

I have a few contributors in my github project. I want to allow only one of them to "push" to master. And this guy is not me (the owner of the repository). Is it possible to do?

我的 github 项目中有一些贡献者。我只想让他们中的一个人“推动”掌握。这家伙不是我(存储库的所有者)。有可能吗?

采纳答案by Sebi

Back then, when this question was posted, GitHub didn't allow you to specify access privileges on a branch level. You can only do it on a repository level. So what you are requesting wasn't possible.

当时,当发布此问题时,GitHub 不允许您指定分支级别的访问权限。您只能在存储库级别执行此操作。所以你的要求是不可能的。

If you want to work around this limitation, I personally see two options:

如果您想解决此限制,我个人会看到两个选项:

  • you could use some kind of commit hooks, etc. to at least inform someone that something wrong happened
  • If you really need this tight control, you could use two repositories, one only holding your "master" branch. You would then need to make sure that only your "guy" gets write access to the master repository
  • 你可以使用某种提交钩子等来至少通知某人发生了错误
  • 如果你真的需要这种严格的控制,你可以使用两个存储库,一个只保存你的“主”分支。然后,您需要确保只有您的“家伙”才能获得对主存储库的写访问权限

I think it is easier to solve that with some organisation. In my team, we never push anything directly to master, but instead always create pull requests. That ensures that at least someone takes a look before code gets merged and you could also define a policy that only your "guy" is allowed to merge pull requests.

我认为通过一些组织来解决这个问题会更容易。在我的团队中,我们从不直接向 master 推送任何内容,而是始终创建拉取请求。这确保至少有人在代码合并之前查看一下,您还可以定义一个策略,只允许您的“人”合并拉取请求。

Update

更新

GitHub now announcedthat they will roll-out a new feature called protected branches. This feature has been present in other git distributions like Atlassian Stash for years. It will allow you to protect some branches from pushes. However, it still doesn't allow complete ACL-based protection of individual branches. So you might want to check this feature out in case you don't want to rely on an organizational solution as outline above.

GitHub 现在宣布他们将推出一项名为受保护分支的新功能。此功能已存在于其他 git 发行版中,例如 Atlassian Stash 多年。它将允许您保护某些分支免受推送。但是,它仍然不允许对单个分支进行完整的基于 ACL 的保护。因此,如果您不想依赖上面概述的组织解决方案,您可能需要检查此功能。

回答by VonC

Note: Protected branches and required status checks(September 3, 2015) will allow you to protect a branch

注意:受保护的分支和所需的状态检查(2015 年 9 月 3 日)将允许您保护分支

  • against forced pushed
  • against deletion
  • against merged changes until required status checks pass
  • 反对强推
  • 反对删除
  • 反对合并的更改,直到所需的状态检查通过

https://cloud.githubusercontent.com/assets/25792/9596474/27db3ce6-502a-11e5-9b19-5b47a8addc65.png

https://cloud.githubusercontent.com/assets/25792/9596474/27db3ce6-502a-11e5-9b19-5b47a8addc65.png



Since March 2016, as commentedby Sanderbelow, you have User and team restrictions

自 2016 年 3 月以来,正如下面Sander评论的,您有用户和团队限制

Sometimes merges to a protected branch are best left to a release manager or a team of people responsible for the stability of that particular branch. Organizations can now specify which members and teams are able to push to a protected branch.

有时合并到受保护分支最好留给发布经理或负责该特定分支稳定性的人员团队。组织现在可以指定哪些成员和团队能够推送到受保护的分支。

https://cloud.githubusercontent.com/assets/4719/14140705/ed98abac-f67a-11e5-951e-b48c842fb77f.png

https://cloud.githubusercontent.com/assets/4719/14140705/ed98abac-f67a-11e5-951e-b48c842fb77f.png

回答by kbuilds

This is exactly what forking was designed for. You would have the main repository protected, and you would allow read permissions on that repository for all contributors. Those contributors would fork the repo, and would push their changes to their personal copies of the main repo. When they are ready to introduce code into the main repository, they would submit a pull request into the main repo. In this case, owners of the main project could complete the pull request.

这正是分叉的目的。您将保护主存储库,并允许所有贡献者对该存储库进行读取权限。这些贡献者会分叉这个 repo,并将他们的更改推送到主 repo 的个人副本。当他们准备好将代码引入主存储库时,他们将向主存储库提交拉取请求。在这种情况下,主项目的所有者可以完成拉取请求。

回答by Malick

We can now use "CODEOWNERS" file to Require review from Code Owners to valid a pull request. We can set different permissions based on their GitHub account.

我们现在可以使用“CODEOWNERS”文件来要求代码所有者以验证拉取请求。我们可以根据他们的 GitHub 账号设置不同的权限。

see hereand here

看到这里这里