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

15,952

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

Solution 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

Solution 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

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.

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

Share:
15,952

Related videos on Youtube

Sherzod
Author by

Sherzod

There are only 10 kinds of people in this world: those who know binary and those who don’t.

Updated on September 16, 2022

Comments

  • Sherzod
    Sherzod over 1 year

    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?

  • Sherzod
    Sherzod almost 12 years
    So in other words, there's no native way of doing it without a hack?
  • Adam Dymitruk
    Adam Dymitruk almost 12 years
    these are not hacks. A blessed repository is a pattern in DVCS. Github can't allow you to install custom code. Before github, you had your own bare repos sitting on an internal server. Sounds like you guys could use a gitolite install.
  • Sherzod
    Sherzod almost 12 years
    Yes, I understand that. We do have local git, but we're planning to switch to github and I was wondering if they have something implemented there for this purpose or is it possible to implement something custom.
  • Adam Dymitruk
    Adam Dymitruk almost 12 years
    not that I know of. They may add that feature. You can use github as auxiliary for code review and other features but keep a master local.
  • schnedan
    schnedan over 2 years
    @AdamDymitruk "A blessed repository is a pattern in DVCS" - Well not really. I never heard of that before git. In mercurial (a real DVCS) you do not need that. In git the branching concept is so dysfunctional that people come up with things like "A blessed repository" or bare repositories as Proxy's. In Fact, gits current defaults make it a VCS without the D? Don't believe? Just push from one PC to another. It will tell you to not corrupt the other repro this is disabled. See... no "D". Git wants its central server repro. The ability to have some of them doesn't make it a DVCS!