'Github new Branch creation restriction

I was wondering if there is anyway to restrict the branch creation in GitHub. I have 4-5 branches created for a repo and would like to maintain it that way. I have applied branch restriction rules on my master branch and hence the developers cannot merge into master but they can create their own branches . Is there a way i can restrict and force developers to use the other branches that are available and not create a new one ?

Note: I use GITHUB Enterprise



Solution 1:[1]

This should be supported now (May 2022).
See:

Block creation of branches that have matching names

Now, admins can block creation of branches that match a configured name pattern.

For example, if a repository's default branch is renamed from master to main, admins can prevent any subsequent creation or push of the master branch so that only the new branch name is used.

Previously, admins could use branch protection rules to restrict who could push to existing branches, but they couldn't block the creation of those branches.
This is now possible using a branch protection setting named Restrict pushes that create matching branches.
To use the setting, create a new branch protection rule with a name pattern that matches the branch name you want to block (e.g. master or not-allowed*). Then, enable the settings Restrict who can push to matching branches and Restrict pushes that create matching branches, as shown here:

An image showing the two branch protection settings to enable for restricting pushes that create matching branches -- https://i0.wp.com/user-images.githubusercontent.com/1767415/166982191-bb0af50c-971a-481c-ae80-a17ef91223c3.png?ssl=1

For more information, visit About protected branches in the GitHub documentation.

We appreciate feedback on this and other topics in GitHub's public feedback discussions.

In your case, adding a rule '*' will prevent the creation of any new branch.

Solution 2:[2]

There is no such option to Restrict branch creation. Branch is a basic necessity of git and therefore everyone is eligible to create a new branch. But certainly, like you said, you can protect some defined set of branches for commits and merge .

Solution 3:[3]

If you want to block creating branches on a repo (I mean not locally). You can achieve it by adding a pre-push git hook from client side to block publishing of branches. But the problem is that every git user have to add it locally and can omit it by simply passing --no-verify to push command.

But it you prefer server-side solution you can create a pre-receive hook on repo to block pushing to non-existing branches. Creating a new remote branch is also a push operation.

And it works also for Github Enterprise as I'm also using it. You can read more here about git hooks in git documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC
Solution 2 Rakmo
Solution 3 Gosia B.