How to Enforce Branch Naming Conventions With Automation
A branch naming convention (feature/, fix/, hotfix/, release/) lets CI route jobs by prefix and reject off-pattern branches automatically.
Consistent prefixes make branch intent machine-readable: CI can deploy previews for feature/*, fast-track hotfix/*, and build candidates on release/*. Enforce the pattern on pull_request so non-conforming branches fail early.
Steps
- Agree on a prefix set, for example
feature/,fix/,hotfix/,release/,chore/. - Validate the source branch name on
pull_request. - Fail the check when the name does not match the pattern.
- Reuse the parsed prefix to drive downstream routing and labels.
Workflow
.github/workflows/branch-name.yml
on:
pull_request:
types: [opened, edited, synchronize]
jobs:
branch-name:
runs-on: ubuntu-latest
steps:
- name: Validate branch name
run: |
BRANCH="${{ github.head_ref }}"
if ! echo "$BRANCH" | grep -Eq '^(feature|fix|hotfix|release|chore)/[a-z0-9._-]+$'; then
echo "Branch '$BRANCH' does not match the naming convention" >&2
exit 1
fiGotchas
- Use
github.head_reffor the source branch on pull_request;github.refis the merge ref. - Keep the pattern documented, or contributors will keep tripping the check.
Related guides
How to Set CI Triggers per Branch With Push and PR FiltersControl which branches run CI using on.push.branches and on.pull_request.branches filters, so feature, develo…
How to Set Up CI for the Forking Workflow in Open SourceRun CI safely for the forking workflow where external contributors open pull requests from forks, keeping sec…
How to Configure Branch Protection Rules per StrategySet branch protection rules that match your branching strategy, requiring status checks, reviews, and linear…