Search code examples
githubpull-requestbranching-and-merging

How to restrict merging options on github


By default github provides three ways to merge a PR to a branch:

  1. Merge Commit
  2. Squash and Merge
  3. Rebase and Merge

Can we add some branch rule or is there any way we can restrict them and allow only one or two options for any given branch.


Solution

  • Q4 2024: As confirmed by Patrick Knight (Product Manager at GitHub) in issue 10809

    We've just shipped our initial version of this internally and are targeting a public preview next month.

    Quick peek at where we are at on the UI front:

    https://private-user-images.githubusercontent.com/7575792/386761030-4ef0b770-09d8-4282-be17-10aae991368f.jpg

    See also:

    And yes, gh CLI will be included:

    Yes it will be in the ruleset API as property on the pull request object called "allowed_merge_types".

    See this example:

          "type": "pull_request",
          "parameters": {
            "required_approving_review_count": 0,
            "dismiss_stale_reviews_on_push": false,
            "required_reviewers": [],
            "require_code_owner_review": false,
            "require_last_push_approval": false,
            "required_review_thread_resolution": false,
            "automatic_copilot_code_review_enabled": false,
            "allowed_merge_types": [
              "squash",
              "merge",
              "rebase"
            ]
          }
        }
      ],```
    

    Q3 2024: While discussion 10809 is still not supported, there are some evolutions:

    On the second point, Dhiraj Sharma proposes the following:

    I know it's better to have this feature in the backend, but for anyone okay with a frontend solution to avoid mistakes when merging PRs, I've built an extension.
    I use squash for all branches except when merging into master, but occasionally, I make mistakes in the process.

    The extension lets you customize settings for repos, source/destination branches, and merge options. Feel free to check it out, and I'd appreciate any feedback or suggestions!

    See GitHub PR Merge Strategy Restrictor.

    https://lh3.googleusercontent.com/rEMCE0j4yi0TVZbaEs99oa2GnG-IdL5Skdr6w07Rx3ZCha6WDyAxf0-DCY3QMHXJwTM9dakeEum0MN5n5K4yaPtT=s800-w800-h500