To begin, be sure your needs is very normal and traditional.
The answer is ... Yes.
How to prevent merging from a branch to another, setting up a server Git Hook
These are some useful links:
To help you (and for fun ^^), I wrote a dedicated hook in Python to reach your specific needs (you just need to adapt FORBIDDEN_SOURCE_BRANCH and FORBIDDEN_IF_NOT_DEST_BRANCH if you want to work with some other branches).
#!/bin/python
##
## Author: Bertrand Benoit <mailto:contact@bertrand-benoit.net>
## Description: Git Hook (server-side) allowing to prevent merge from some branches to anothers
## Version: 0.9
import sys, subprocess, re
FORBIDDEN_SOURCE_BRANCH='testing'
FORBIDDEN_IF_NOT_DEST_BRANCH='master'
# Considers only merge commit.
if not (len(sys.argv) >=2 and sys.argv[2] == 'merge'):
sys.exit(0)
# Defines which is the source branch.
with open(sys.argv[1], 'r') as f:
mergeMessage=f.readline()
mergeBranchExtract=re.compile("Merge branch '([^']*)'.*$").search(mergeMessage)
if not mergeBranchExtract:
print('Unable to extract branch to merge from message: ', mergeMessage)
sys.exit(0) # Ensures normal merge as failback
# Checks if the merge (source) branch is one of those to check.
mergeBranch=mergeBranchExtract.group(1)
if mergeBranch != FORBIDDEN_SOURCE_BRANCH:
sys.exit(0) # It is NOT the forbidden source branch, so keeps on normal merge
# Defines which is the current branch.
currentBranchFullName=subprocess.check_output(['git', 'symbolic-ref', 'HEAD'])
currentBranchExtract=re.compile("^.*/([^/]*)\n$").search(currentBranchFullName)
if not currentBranchExtract:
print('Unable to extract current branch from: ', currentBranchFullName)
sys.exit(1) # Ensures normal merge as failback
# Checks if the current (destination) branch is one of those to check.
currentBranch=currentBranchExtract.group(1)
if currentBranch != FORBIDDEN_IF_NOT_DEST_BRANCH:
print("FORBIDDEN: Merging from '" + mergeBranch + "' to '" + currentBranch + "' is NOT allowed. Contact your administrator. Now, you should use git merge --abort and keep on your work.")
sys.exit(1) # This is exactly the situation which is forbidden
# All is OK, so keeps on normal merge
sys.exit(0)
To share all this work, I created a new Gitlab repository, in which I'll add further hooks when needed :)
For information, you can also setup protected branches to keep them safe from some users
This is the complete documentation about that.
Let me know if you need further help.