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

After the first release of our product, we will move on to other sections for basic design and feature development. Is there a way to create a branch in such a way that we can protect it from deletion (accidentally or as intended) if you are not a specific user (based on the role or username)?

I tried to create a sample git repository in our local gitlab machine, and then protected one of the branches from this option on the website, but then was able to remove it with git push origin :branch_name . Thanks in advance!

Will the solution work on github.com?

+6
source share
5 answers

There are many ways to solve this problem:

  • Make another repo, which is a sandbox, and give read-only access to the main thing. If they accidentally delete, they can get a branch from the master repo. It is assumed that you only use github for your repositories, away from the local repositories.
  • Set hooks in the repository that do not allow branches to be deleted unless you are a specific user. You cannot do this on github, as they will not allow arbitrary code to be executed on your servers. If you get a local repo, you can do it.
  • Install a local gitolite installation to manage branch with permissions.
+5
source

You can use branch permissions on the server by adding a pre-receive hook that protects your repository and adds something like this to your configuration file in your bare repository:

 [hooks] allowedtomerge = user1,user2,user3 protectedbranches = master 
+4
source

Since the OP shershams mentioned in the comments

we plan to switch to github and I was wondering if they have something for this purpose

It turns out that GitHub has implemented (and is available soon):

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

  • against push
  • against removal
  • against merged changes until status checks pass

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

+3
source

I have a branch like git that has dev / master / production branches used for step deployments, so there are branches that I want to protect from being deleted. I use pull requests and Visual Studio Team Services, so after each transfer request from dev to master, for example, VSTS asks if I want to remove the source branch (dev).

I was worried that the developer accidentally deleted the dev or other important branch that is used for deployments, so I used this hack:

I created a developer branch called "save", made a one-line change, opened a pull request against dev and just left it open.

As long as there is an open pull request against dev, dev cannot be deleted, and VSTS will not ask if I want to delete the branch.

If there is any other official solution to this problem, I would be happy to use it. But at the moment it was easy and works.

0
source

If you use Gitlab , you can configure secure branches :

Configure Secure Branches

To protect the branch, you need to have at least a master level. Note that the main branch is protected by default.

  • Go to your project Settings ➔ Repository

  • Highlight the Protected branches section.

[...]

0
source

Source: https://habr.com/ru/post/920064/


All Articles