All guides

GitHub rulesets: protect branches and tags

Only one classic branch protection rule applies to a branch at a time. Rulesets stack, cover tags and pushes as well as branches, and can be read by anyone with read access to the repository.

A laptop showing a stylesheet in a code editor

Rulesets and classic branch protection

Both protect branches, and both stay in force when they overlap. GitHub's documentation lists what rulesets add:

  • Several rulesets can apply to one branch at the same time. Only one branch protection rule applies.
  • A ruleset has an enforcement status, so you can switch it off and keep its definition.
  • Anyone with read access can view the active rulesets, so auditors can review protections with no administrator access.

Rulesets are available in public repositories on GitHub Free, and in public and private repositories on GitHub Pro, Team and Enterprise Cloud. A repository can hold up to 75 of them.

Three targets

  • Branch rulesets select branches with fnmatch patterns. releases/**/* targets every branch whose name starts with releases/. In the REST API, ~DEFAULT_BRANCH targets the default branch and ~ALL targets all branches.
  • Tag rulesets do the same for tags. With the rules "Restrict updates" and "Restrict deletions", only users with bypass permissions can push to or delete the matching tags.
  • Push rulesets need no branch target, because they apply to every push to the repository and its whole fork network. They block pushes by file path, path length, file extension or file size. They cover private and internal repositories. The documentation lists them for the GitHub Team plan, and its Enterprise Cloud edition lists them for GitHub Enterprise Cloud.

Enforcement statuses

A ruleset is Active or Disabled. The Enterprise Cloud documentation adds a third status, Evaluate: the ruleset is not enforced, and the Rule Insights page shows which actions would have violated its rules. The REST reference states that evaluate is only available with GitHub Enterprise.

Bypass lists

Each ruleset has a bypass list. Eligible for it are repository admins, organization and enterprise owners, the maintain or write role, teams, GitHub Apps and Dependabot.

Every entry has a mode, and the default is "Always allow". "For pull requests only" grants bypass with no direct push: the actor must open a pull request and can then choose to bypass the protections and merge it. That leaves a trail in the pull request and the audit log.

How rulesets layer

Rulesets have no priority. When several target the same branch, GitHub combines their rules, and where the same rule appears twice the most restrictive version wins. Classic branch protection rules join the same calculation.

GitHub's example: a ruleset requires signed commits and three reviews, and a branch protection rule on the same branch requires a linear history and two reviews. The branch then requires signed commits, a linear history and three reviews. A new ruleset can make the rules more restrictive, but never less restrictive.

To see the outcome for a branch, run gh ruleset check main. It returns all rules that apply to that branch name, regardless of where they are configured.

Organization rulesets

An organization owner can create a ruleset that targets many repositories: all of them, a selected list, or a filter by naming convention, deployment context or custom properties.

GitHub's pages differ on the plan. "Creating rulesets for repositories in your organization" says the feature is for customers on GitHub Team or GitHub Enterprise plans. "About rulesets" says that organizations on the GitHub Enterprise plan can set up rulesets at the organization level. Check your own organization's settings.

Only organization owners can edit such a ruleset. Repository admins can still add rulesets of their own, and those are aggregated with the organization's.

A ruleset for main as JSON

JSON

{
  "name": "main",
  "target": "branch",
  "enforcement": "active",
  "conditions": {
    "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] }
  },
  "rules": [
    { "type": "deletion" },
    { "type": "non_fast_forward" },
    {
      "type": "pull_request",
      "parameters": {
        "required_approving_review_count": 1,
        "dismiss_stale_reviews_on_push": true,
        "require_code_owner_review": false,
        "require_last_push_approval": true,
        "required_review_thread_resolution": true
      }
    },
    {
      "type": "required_status_checks",
      "parameters": {
        "strict_required_status_checks_policy": false,
        "required_status_checks": [{ "context": "test" }]
      }
    }
  ]
}

The field names come from the REST reference. These four rules restrict deletions, block force pushes, require a pull request and require a status check. Every value under parameters, including the check name test, is a choice to replace with your own. { "type": "required_signatures" } adds the rule that commits must have verified signatures.

Create the ruleset through the REST API, and read one back the same way:

Shell

gh api --method POST repos/my-org/my-repo/rulesets --input main.json
gh api repos/my-org/my-repo/rulesets/<ruleset-id>

The web interface imports a ruleset from a JSON file: under Settings, Rulesets, select New ruleset, then Import a ruleset, and open the exported JSON file. For the export, the Enterprise Cloud documentation describes the ruleset history: you can download a JSON file of the configuration at a specific iteration, with the bypass list excluded. GitHub publishes prebuilt rulesets in github/ruleset-recipes.

What to check

  • Status checks. After you enable required status checks, all of them must pass before collaborators can merge. Strict checks, where the branch must be up to date before merging, are the default.
  • Old branch protection rules. They still layer with your rulesets. Under Settings, Branches, the "Convert to ruleset" action proposes one or more rulesets for a branch protection rule.
  • Force pushes and the default branch. A ruleset that blocks force pushes also stops owners and administrators from changing or renaming the default branch, unless they hold bypass rights for that ruleset.
  • The bypass list. Prefer the pull request mode, which leaves the trail described above.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides