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

后端 未结 5 1985
情歌与酒
情歌与酒 2021-02-19 20:29

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

相关标签:
5条回答
  • 2021-02-19 21:08

    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
    
    0 讨论(0)
  • 2021-02-19 21:09

    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.

    [...]

    0 讨论(0)
  • 2021-02-19 21:25

    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.
    0 讨论(0)
  • 2021-02-19 21:25

    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.

    0 讨论(0)
  • 2021-02-19 21:33

    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.

    0 讨论(0)
提交回复
热议问题