git 优雅的解决方案,以防止仅对 master 进行强制推送

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

Elegant solution to prevent force push on master only

gitgithubgit-push

提问by Swift

I'm trying to figure out a good way to prevent developers from force pushing master with Git. We used GitHub to host our remote repositories so a pre-receivehook isn't an option. Any other solutions that could easily be implemented for a team of developers?

我试图找出一种防止开发人员使用 Git 强制推送 master 的好方法。我们使用 GitHub 来托管我们的远程存储库,因此pre-receive无法选择挂钩。任何其他可以为开发团队轻松实施的解决方案?

As a side note, I don't want to disable force pushing in general. Sometimes it is a necessary evil. But that said, force pushing on master cannot happen.

作为旁注,我一般不想禁用强制推送。有时它是一种必要的邪恶。但话虽如此,强推主人是不可能发生的。

回答by manojlds

Update

更新

Github has since introduced the concept of protected branches. It can be found under Settings-> Branches-> Protected Branches

Github 此后引入了受保护分支的概念。它可以在Settings-> Branches->下找到Protected Branches

This "protection" can be enabled for any branch, and for any user, including admins.

可以为任何分支和任何用户(包括管理员)启用这种“保护”。

More details here - https://help.github.com/articles/defining-the-mergeability-of-pull-requests/

更多细节在这里 - https://help.github.com/articles/defining-the-mergeability-of-pull-requests/

You cannot prevent this in Github.

你无法在 Github 中阻止这种情况。

What you can do is have an intermediate repo on your side, run a pre-receive hook in that to prevent force push and push from this intermediate repo to github and block access for direct push to github. Yeah this is not elegant and you lose lots of features with Github, but I don't see any other way.

您可以做的是在您身边有一个中间存储库,在其中运行一个预接收钩子,以防止从这个中间存储库强制推送和推送到 github 并阻止直接推送到 github 的访问。是的,这并不优雅,而且你在 Github 上失去了很多功能,但我看不出有任何其他方式。

Edit: Just came across this answer, which says the same and gives another workaround: GitHub - prevent collaborators from using push -f

编辑:刚刚遇到这个答案,它说的是相同的,并提供了另一种解决方法:GitHub - 阻止合作者使用 push -f

回答by Lukasz Wiktor

GitHub introduced a new feature called "Protected Branches" to prevent force pushing. You can configure it in repository Settings > Branches.

GitHub 引入了一项名为“受保护分支”的新功能,以防止强制推送。您可以在存储库设置 > 分支中配置它。

github protected branches

github 受保护的分支

回答by triad

I'm not a github expert, but I believe you can do this if you use github Organizations.

我不是 github 专家,但我相信如果您使用 github Organizations ,您可以做到这一点。

You would need to create an Organization "Pull Only" Team for the developers who aren't allowed to push to master. Instead of working on the main repo, they would fork your main repo and set their local repos to sync with their fork repos. Anytime they want to push code to the main repo, they would have to push their code onto their fork and submit a Pull Requestinstead.

您需要为不允许推送到掌握的开发人员创建一个组织“仅拉取”团队。他们不会在主存储库上工作,而是分叉您的主存储库并将其本地存储库设置为与他们的分叉存储库同步。任何时候他们想要将代码推送到主存储库,他们都必须将他们的代码推送到他们的 fork 并提交一个Pull Request

This is where you create another Organization "Push & Pull" Team for developers which can review and approve the pull requests, and merge them into your main repo.

在这里,您可以为开发人员创建另一个组织“推拉”团队,该团队可以和批准拉取请求,并将它们合并到您的主存储库中。

Basically every developer is only pushing to their own fork, and nobody can push to the main repo directly (except the Team with both Push/Pull access). For a push to happen on the main repo, it must always go through a pull request from a fork repo.

基本上每个开发人员都只推送到他们自己的分支,没有人可以直接推送到主存储库(除了具有 Push/Pull 访问权限的团队)。要在主存储库上进行推送,它必须始终通过来自 fork 存储库的拉取请求。

回答by Wojtek Kruszewski

[UPDATE] Github now provides "protected branches"feature just for this purpose.

[更新] Github 现在为此提供了“受保护的分支”功能。

Easy-peasy: Get Github "Enterprise" installation, then follow instructions below: https://help.github.com/enterprise/2.1/admin/articles/blocking-force-pushes-to-a-repository/

Easy-peasy:获取 Github“企业”安装,然后按照以下说明操作:https: //help.github.com/enterprise/2.1/admin/articles/blocking-force-pushes-to-a-repository/

Disclaimer: this was a tounge-in-cheek answer. Drop a line at https://github.com/contactand maybe Github folk will hear us and enable this option for the masses.

免责声明:这是一个诙谐的回答。在https://github.com/contact 上留言,也许 Github 的人会听到我们的声音并为大众启用这个选项。

回答by VxJasonxV

In the case of GitHub specifically, it is possible to block force pushes, but it is not possible to make this change on your own. You'll have to contact support ( [email protected], https://github.com/contact) to make the change on repositories you specify.

特别是在 GitHub 的情况下,可以阻止强制推送,但您无法自行进行此更改。您必须联系支持人员 ( [email protected], https://github.com/contact) 才能对您指定的存储库进行更改。

回答by Alejandro L.Rocha

Well if you don't want to change the branch protection, or you might have some group in git that is allow to push to master, I think is better to have a documentation on how to configure github to your Team members in their machines with this command, and telling them the purpose of the commands.

好吧,如果您不想更改分支保护,或者您可能在 git 中有一些允许推送到 master 的组,我认为最好有一份关于如何为您的团队成员在他们的机器上配置 github 的文档这个命令,并告诉他们这些命令的目的。

Supposing that they have forked the repo, add the main repo remote + disallow push to master.

假设他们已经分叉了 repo,添加主 repo remote + disallow push to master。

git remote add upstream [email protected]:username/project.git
git remote set-url --push upstream no_push

is an easy and nice way to prevent it

是一种简单而好的方法来防止它