Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing only certain branches to PR merge to main(master) branch

Tags:

github

I'm looking for methods to protect main branch by preventing branches except dev branch from pull request merge to main branch. But I could not find a way to it. Is there any way for it? or is it possible using Github Action?

like image 954
wallah Avatar asked Nov 28 '25 10:11

wallah


2 Answers

I took Rob Bos's advice and created a workflow to do this, here is what I have in my enforcer.yaml file

name: 'Check Branch'

on:
  pull_request:

jobs:
  check_branch:
    runs-on: ubuntu-latest
    steps:
      - name: Check branch
        if: github.base_ref == 'main' && github.head_ref != 'dev'
        run: |
          echo "ERROR: You can only merge to main from dev."
          exit 1

Then I have the workflow check_branch as a required status check in the branch protection rule for my main branch. Attempting a pull request into main from any branch other than dev fails.

like image 86
J W Avatar answered Nov 30 '25 05:11

J W


That's not possible out of the box. You can write a workflow that runs on a PR and fails if the source branch is not dev. Then add this workflow as a required check in the branch protection rule for the main branch.

like image 20
Rob Bos Avatar answered Nov 30 '25 06:11

Rob Bos



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!