Blog

Restrict GitHub branches to specific prefixes

06 Feb, 2024
Xebia Background Header Wave

Many people follow some kind of naming format for their branches. Be it because they’re using GitHub Flow or Git Flow or because they’ve created their own meaningful naming patterns.

With the new Rulesets you can enforce branches with specific patterns. It was a bit confusing to me at first, because I had to think in inverse to make this rule work.

If you are on GitHub Enterprise you can use the metadata restrictions feature of the new Repository Rulesets feature:

Use of metadata restrictions in GitHub Enterprise Repository rulesets.

But if you are on a different plan (free, team), here’s a trick that will also do the job.

Create a rule that restricts the creation of any branch **, then selectively excludes prefixes from this restriction:

Include pattern:

  • **

Exclude pattern:

  • fix/*<em>/</em>
  • release/*<em>/</em>
  • main

Then turn on ✅ Restrict creations.

Use include and exclude patterns to restrict branch prefixes.

In case you need specific GitHub apps (such as Dependabot and Renovate or Azure Pipelines) to create arbitrary branch names, you can exclude them from the policy:

Use the Add Bypass feature to allow select GitHub Apps to bypass the restriction.

Title photo by : Train Light Signal, used under Creative Commons

Jesse Houwing
Jesse is a passionate trainer and coach, helping teams improve their productivity and quality all while trying to keep work fun. He is a Professional Scrum Trainer (PST) through Scrum.org, Microsoft Certified Trainer and GitHub Accredited Trainer. Jesse regularly blogs and you'll find him on StackOverflow, he has received the Microsoft Community Contributor Award three years in a row and has been awarded the Microsoft Most Valuable Professional award since 2015. He loves espresso and dark chocolate, travels a lot and takes photos everywhere he goes.
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts