git GitHub - 防止协作者使用 push -f

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

GitHub - prevent collaborators from using push -f

gitgithubgit-push

提问by Alex G

Is there a way to prevent force push into a branch or repository?

有没有办法防止强制推送到分支或存储库?

I want to stop important branches from having their history rewritten either accidentally or intentionally. How do people deal with this in larger development teams?

我想阻止重要分支的历史被意外或故意改写。在更大的开发团队中,人们如何处理这个问题?

Ideally, in my view, would possible to lock few branches per repository, and prevent everyone, other than the repository owner from doing a force push into them.

理想情况下,在我看来,可以锁定每个存储库的几个分支,并防止除了存储库所有者之外的每个人都对它们进行强制推送。

采纳答案by J?rg W Mittag

This is easy to do with Git with a pre-receivehook. Of course, this requires that you are actually ableto install hooks, and for obvious reasons, GitHub doesn't allow you to upload arbitrary executable files to run on their servers :-)

使用带有pre-receive钩子的Git 很容易做到这一点。当然,这需要你真的能够安装钩子,而且很明显,GitHub 不允许你上传任意可执行文件在他们的服务器上运行:-)

In general, the workflow with Git or really any distributed version control system, is that you don't allow other people to push to your repository. Instead, you pull from theirs. This requires a much lower level of trust. So, this would be workaround number 1: don't let them push, have them fork and then pull from them. That way, you can control what goes into your repository.

一般来说,Git 或任何分布式版本控制系统的工作流程是不允许其他人推送到您的存储库。相反,你从他们那里拉。这需要低得多的信任级别。所以,这将是解决方法 1:不要让他们推动,让他们分叉,然后从他们身上拉出来。这样,您就可以控制进入存储库的内容。

Another workaround would be to set up your own staging repository on a server you own, where you can install your own Git hooks. You can configure a pre-receivehook which denies pushing if it's not a fast-forward and post-receivehook which automatically forwards all pushes to GitHub. Of course, this means that you lose many of the benefits of using GitHub in the first place.

另一种解决方法是在您拥有的服务器上设置您自己的临时存储库,您可以在其中安装自己的 Git 挂钩。您可以配置一个pre-receive钩子,如果它不是快进和post-receive钩子,则拒绝推送,并自动将所有推送转发到 GitHub。当然,这意味着您首先失去了使用 GitHub 的许多好处。

As a third workaround, you could use multiple repositories. This is a combination of the two other approaches: have one repository that your collaborators can push to and another one that only you have access to, that you pull into from the first repository.

作为第三种解决方法,您可以使用多个存储库。这是另外两种方法的组合:拥有一个您的协作者可以推送到的存储库和另一个只有您有权访问的存储库,您可以从第一个存储库中拉入。

At any rate, you should file a feature request with GitHub (especially if you are a paying customer!) since this seems to be a useful feature.

无论如何,您应该向 GitHub 提交功能请求(特别是如果您是付费客户!),因为这似乎是一个有用的功能。

回答by fnkr

GitHub announced the "protected branches" feature:

GitHub 宣布了“受保护的分支”功能:

[...] a new feature called Protected Branches which gives repository administrators the ability to disable force pushes to specific branches. [...] go to the Branches tab in repository settings and protect branches.

[...] 一个名为 Protected Branches 的新功能,它使存储库管理员能够禁用强制推送到特定分支。[...] 转到存储库设置中的分支选项卡并保护分支。

Source: https://github.com/blog/2051-protected-branches-and-required-status-checks

来源:https: //github.com/blog/2051-protected-branches-and-required-status-checks

Repository >Settings >Branches

存储库 >设置 >分支