GIT: How to protect the branch from being removed by other developers?

会有一股神秘感。 提交于 2021-02-07 05:21:18

问题


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!

Will the solution work on github.com?


回答1:


There are many ways to tackle this:

  1. 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.
  2. 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.
  3. Setup a local gitolite install to manage branches with permissions.



回答2:


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:

[hooks]
        allowedtomerge = user1,user2,user3
        protectedbranches = master



回答3:


Since the OP shershams mentioned in the comments

we're planning to switch to github and I was wondering if they have something implemented there for this purpose

It turns out there is something implemented (and available soon) in GitHub:

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

  • 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.




回答4:


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).

I was worried about a developer accidentally deleting dev or another important branch which is used for deployments so I used this 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.

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.

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.




回答5:


If you are using Gitlab then 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.

  1. Navigate to your project's Settings ➔ Repository

  2. Scroll to find the Protected branches section.

[...]



来源:https://stackoverflow.com/questions/11401155/git-how-to-protect-the-branch-from-being-removed-by-other-developers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!