Search code examples
gitgit-workflow

Disallow branching from and merge of specific branches


Is there a git hook or some other way to prohibit branching from and merging of specific branches. We want to make sure that we don't merge a "dirty" integration branch into our clean deployment branch.

The major goal is that people cannot execute something like this:

git checkout integration
git checkout -b major_problem_branch

or

git checkout deployment_or_hotfix_or_feature_branch
git merge integration

Solution

  • You can use branch permissions on the server by adding this to the config file in your bare repository:

    [hooks]
            allowedtomerge = user1,user2,user3
            protectedbranches = master
    

    That will allow user1, user2, and user3 to merge to master branch, but no one else.

    Or you could create a pre-commit hook:

    #!/bin/bash
    
    if [[ `git symbolic-ref HEAD` == "refs/heads/master" ] -a ["$USER" != "user1"]] then
        echo "You cannot commit in master!"
        exit 1
    fi
    

    Then you have people that evaluate and allow changes to go forward.

    Ideally, I would use a system that you like, for instance gerrit or assembla or github. They have nice ways of controlling a master branch through Merge Requests.