Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict branch names on GitHub

Tags:

github

How do I avoid developers from create branches that doesn't follow one of the patterns below?

user-story/W-*

bugfix/W-*

like image 490
Saulo Avatar asked May 18 '26 12:05

Saulo


2 Answers

GitHub has branch protection rules which can restrict certain branches, but it doesn't have negative patterns, so there's no way to prevent all patterns but given ones.

Depending on your goal, you can implement a couple of approaches:

  • You can set up a CI rule to reject incorrectly named branches. If you protect the main branch and require that check to be green, then people won't be able to merge them.
  • You can force users to use a forking model to prevent them from polluting the main repository with invalidly named branches.

I will point out that your proposed policy has the downside that if someone is working on something experimental that doesn't fit into a bug fix or user story, then blocking the creation of those branches altogether (if it were possible) would prevent users from pushing that data to the remote. I've often ended up doing this and at times this work has ended up being really useful, potentially as a base for future work.

like image 178
bk2204 Avatar answered May 20 '26 16:05

bk2204


I'm aware that this is an older question, but I just came across it while looking for a solution myself. I ended up using a wildcard branch protection rule to achieve the same effect.

In your example, this would include creating branch protection rules for user-story/W-* and bugfix/W-*, and then a third rule for **/**. This third rule will apply to all protection rules not caught by the earlier two.

In the third rule, which matches **/**, you set the protection rule to 'lock branch'. This will ensure all branches that get created without matching one of the other patterns, become read-only.

This is not a perfect solution as the branches still get created, but at least you can ensure these branches cannot get used, which should motivate your developers to use proper branch naming.

like image 30
Tijmen Avatar answered May 20 '26 15:05

Tijmen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!