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

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.

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

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

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.


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