I want to control creation / deletion of branches that come off master (locally)

I want to create specific set of branches off of master during initial set up and prevent them from being deleted or any new branch from being created off of master. Any new branches created or deleted after that will be done off of those branches that were made during setup. Is there a way to do this? Git hooks or something else? I would like it where if git branch -d < branch-to-protect > is run then I want it to be prevented from deletion and possibly echo a message to the screen. Also, if git branch < any-new-branch > or git branch master < any-new-branch > or git checkout -b < any-new-branch> is run then check if it would be coming off of master; and, if so, stop it and possible echo a message to the screen.

Example:

Suppose I create branches “Development” and “Staging” directly off of master during initial setup. After this I do not want allow any other branch to exist directly off of master, only off of Development or Staging only. I also do not want Development or Staging to be able to delete it.

Thanks in advance if any help

Any thoughts?

Hi,

you can define protected branches with names and patterns in the project settings, even if they are not yet existing.

In terms of preventing to branch off a specific git branch like master - I don’t think that you can do that easily. Possibly you can do it in a later step with verifying this during MR review and comparing the branch history/diff.

Cheers,
Michael

I’m not sure if it’s possible to use conditionals with a command alias in linux; but, if it is, what about making an alias to the relevant git command that create or delete branches and use a conditional statement to check where the branch is being created or what the branch name is that being deleted and act accordingly?

What If if were to put some bash script into my .bashrc that runs a check every time git branch or git checkout -b or git branch <source-branch> <branch-name> is run and makes sure that the new branch is not being created on master? Basically, could I code for it in some configuration file so that certain commands get subjected to conditions?