GIT:如何保护分支不被其他开发者移除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11401155/
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
GIT: How to protect the branch from being removed by other developers?
提问by Sherzod
After the first release of our product, we will be switching to a different branches for the main development and feature development. Is there a way to create a branch in such a way, so that we can protect it from being removed (accidentally or on purpose) unless you're a specific user (based on role or username)?
在我们的产品首次发布后,我们将切换到不同的分支进行主要开发和功能开发。有没有办法以这种方式创建分支,以便我们可以保护它不被删除(意外或故意),除非您是特定用户(基于角色或用户名)?
I tried to create a sample git repository in our local gitlab machine, then protected one of the branches from the option on the website, but then I was able to remove it with git push origin :branch_name
. Thanks in advance!
我尝试在我们本地的 gitlab 机器中创建一个示例 git 存储库,然后从网站上的选项中保护其中一个分支,但随后我能够使用 .gitlab 将其删除git push origin :branch_name
。提前致谢!
Will the solution work on github.com?
该解决方案是否适用于 github.com?
采纳答案by Adam Dymitruk
There are many ways to tackle this:
有很多方法可以解决这个问题:
- Make another repo that's a sand box, and give readonly access to the master one. If they delete by accident they can get the branch from the master repo. This assumes you are only using github for your repos aside the local dev repos.
- Setup hooks in the repository that don't allow branches to be deleted unless you are a specific user. You can't do that on github as they won't allow arbitrary code to be executed on their servers. If you get a local repo instead, you can do this.
- Setup a local gitolite install to manage branches with permissions.
- 制作另一个沙盒存储库,并授予对主存储库的只读访问权限。如果他们不小心删除,他们可以从主存储库中获取分支。这假设您仅将 github 用于您的存储库,而不是本地开发存储库。
- 除非您是特定用户,否则在存储库中设置不允许删除分支的挂钩。你不能在 github 上这样做,因为他们不允许在他们的服务器上执行任意代码。如果您改为获得本地存储库,则可以执行此操作。
- 设置本地 gitolite 安装以管理具有权限的分支。
回答by Michael
You can use branch permissions on the server by adding a pre-receive hook that protects your repository and add something like this to your config file in your bare repository:
您可以通过添加一个 pre-receive 钩子来保护您的存储库并将类似的内容添加到您的裸存储库中的配置文件中,从而在服务器上使用分支权限:
[hooks]
allowedtomerge = user1,user2,user3
protectedbranches = master
回答by VonC
Since the OP shershamsmentioned in the comments
由于评论中提到的OP shershams
we're planning to switch to github and I was wondering if they have something implemented there for this purpose
我们计划切换到 github,我想知道他们是否为此实施了一些东西
It turns out there is something implemented (and available soon) in GitHub:
事实证明,在 GitHub 中实现了一些东西(并且很快可用):
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
- 反对强推
- 反对删除
- 反对合并的更改,直到所需的状态检查通过
Note that, since Dec. 4th 2019, you can grant all users with push access the ability to delete a protected branch by enabling Allow deletions
.
请注意,自 2019 年 12 月 4日起,您可以通过启用Allow deletions
.
回答by Asher Garland
I have a git branch model which has dev/master/production branches used for staged deployments, so there are branches that I want protected against deletion. I use pull requests and Visual Studio Team Services, so after each pull request from dev to master for example, VSTS would ask if I would like to delete the source branch (dev).
我有一个 git 分支模型,它有用于分阶段部署的 dev/master/production 分支,所以我想要保护一些分支不被删除。我使用拉取请求和 Visual Studio Team Services,因此在每次从 dev 到 master 的拉取请求之后,VSTS 会询问我是否要删除源分支 (dev)。
I was worried about a developer accidentally deleting dev or another important branch which is used for deployments so I used this hack:
我担心开发人员不小心删除了 dev 或另一个用于部署的重要分支,所以我使用了这个 hack:
I created a branch off of dev called "save", made a one line change, opened a pull request against dev, and just leave it open.
我创建了一个名为“save”的 dev 分支,进行了一行更改,打开了一个针对 dev 的拉取请求,然后让它保持打开状态。
As long as there is an open pull request against dev, dev cannot be deleted and VSTS will not ask if I would like to delete the branch.
只要有针对 dev 的 open pull request,dev 就不能删除,VSTS 也不会询问我是否要删除分支。
If there is any other more official solution to this problem, I'd be happy to use it. But for now, this was easy and works.
如果这个问题还有其他更官方的解决方案,我很乐意使用它。但就目前而言,这很容易并且有效。
回答by kinjelom
If you are using Gitlabthen you can configure protected branches:
Configuring protected branches
To protect a branch, you need to have at least Master permission level. Note that the master branch is protected by default.
Navigate to your project's Settings ? Repository
Scroll to find the Protected branches section.
[...]
配置受保护的分支
要保护分支,您至少需要具有 Master 权限级别。请注意,默认情况下主分支受到保护。
导航到您项目的设置?存储库
滚动以找到受保护的分支部分。
[...]