Featured image of post Notes on Protecting the main Branch on GitHub

Notes on Protecting the main Branch on GitHub

Overview Link to this heading

These notes cover how to configure GitHub rulesets to protect the main branch.

Background Link to this heading

GitHub rulesets let you apply rules to branches.

I have summarized what each rule means in a separate post.

This post focuses on which rules to enable to protect the main branch.

Rules Link to this heading

Block the creation of branches with main-like names Link to this heading

Enable “Restrict creations” for branches matching main*. The trailing * is important here. This blocks the creation of branches whose names could be confused with main, such as main2 or main-old.

Block all updates to the main branch itself Link to this heading

Enable “Restrict updates” for main. This blocks all updates, including pushes, rebases, and merges.

Block the deletion of the main branch Link to this heading

Enable “Restrict deletions” for main.

Require a successful deployment before merging Link to this heading

Enable “Require deployments to succeed” for main. Merging is blocked until the deployment to the specified environment succeeds.

Require signed commits Link to this heading

Enable “Require signed commits” for main. Note that with this setting, if the pull request branch contains any unsigned commits, you can’t merge the pull request, even by squashing the commits.

Require a pull request (PR) Link to this heading

Enable “Require a pull request before merging” for main.

  • If you are the only developer, set “Required approvals” to 0. If you want to require approval from another reviewer before merging, set “Required approvals” to 1 or more.
  • I recommend enabling “Dismiss stale pull request approvals when new commits are pushed”.
  • If you want to merge your own PRs without a review while requiring your review on everyone else’s PRs, create a CODEOWNERS file and enable “Require review from Code Owners”.
  • Enabling “Require conversation resolution before merging” might be a good idea too.

Require CI/CD to pass Link to this heading

Enable “Require status checks to pass” for main.

  • Enabling “Require branches to be up to date before merging” is the best choice.

Block force pushes Link to this heading

Enable “Block force pushes” for main.

Rules for Public Repositories Link to this heading

  • Enable “Require deployments to succeed” for the branch you deploy from.
  • If you use signed commits, enable “Require signed commits” for the branches you want to protect.
  • Enable “Require a pull request before merging” for the branches you want to protect.

References Link to this heading

Licensed under CC BY-NC-SA 4.0
Last updated on Nov 14, 2026