0

Tell me please how to deny create branch, if branch not created from a master?

I was planning to do to forbid the commit if the branch is not created from the master, but clearly determine that the branch is not created by the wizard does not work.

If the command is executed git checkout -b, he write - created from HEAD.

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Welcome to Stack Overflow! You can take the [tour] first and learn [ask] a good question and create a [mcve]. That makes it easier for us to help you. – Katie Feb 06 '17 at 14:13
  • 1
    Unlike the first comment implies, I find your question quite understandable so far, but please explain *where* you want to forbid. Do you want the server to reject a new branch when it's not based on master? Do you want to prevent the 'git checkout -b' straight away as it's used? (No matter which way you want it, I can tell you now that it's not that easy, but I'll do my best to explain the details if you do respond.) – Jan Krüger Feb 06 '17 at 15:12
  • Hi, I coped with the task. Implemented as follows: I made on a hook pre-commit:
    #!/bin/sh
    
    current_branch=`git rev-parse --abbrev-ref HEAD`
    result=`git reflog --date=local $current_branch | grep -F "Created from master"`
    
    if test $current_branch = "master" ; then
      echo "Cannot commit on master. Create branch and after merge with master!"
      exit 1
    fi
    
    if [ -z "$result" ]; then
     echo "ERROR! A branch is not created from the master! Create a branch with an indication of the parent branch."
        exit 1
    fi
    – Nikolay Revin Feb 07 '17 at 13:47

1 Answers1

0

I coped with the task. Implemented as follows:

I made on a hook pre-commit and obliged command to create branches from command git co -b new_branch_name master or git branch new_branch_name.

#!/bin/sh

current_branch=`git rev-parse --abbrev-ref HEAD`
result=`git reflog --date=local $current_branch | grep -F "Created from master"`

if test $current_branch = "master" ; then
  echo "Cannot commit on master. Create branch and after merge with master!"
  exit 1
fi

if [ -z "$result" ]; then
    echo "ERROR! A branch is not created from the master! Create a branch with an indication of the parent branch."
    exit 1
fi