Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 376
Sherzod Avatar asked Jul 09 '12 18:07

Sherzod


People also ask

How do I protect a branch from being deleted?

By default if you create a Branch protection rule for any branch, it Disables force-pushes to all matching branches and prevents them from being deleted . So if you create a rule with the pattern master , it would prevent the master branch from deletion by default.

How do you make a branch Undeletable?

In your project, select the "Settings" tab on the far right of the menu. In the menu on the left hand side of the "Settings" page, select "Branches". Under the "Protected Branches" section, select any branch you wish from force push and deletion. Thanks!

How do you protect a branch to develop?

Configuring protected branches Scroll to find the Protected branches section. From the Branch dropdown menu, select the branch you want to protect and click Protect. In the screenshot below, we chose the develop branch. Once done, the protected branch will appear in the "Protected branches" list.


2 Answers

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.
like image 155
Adam Dymitruk Avatar answered Oct 21 '22 14:10

Adam Dymitruk


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
like image 42
Michael Avatar answered Oct 21 '22 16:10

Michael