On this page
  1. Rulesets vs classic branch protection rules
  2. A production branch ruleset
  3. Signed commits
  4. CODEOWNERS that actually protect something
  5. Two-person integrity and its limits
  6. Push rulesets
  7. Verify the result
  8. References

The default branch is what you ship. If one person can push to it directly, force-push over its history or merge their own pull request without anyone looking, every other control in your pipeline sits on a single account. Branch protection exists to require a second pair of eyes and a passing build before code lands, and repository rulesets are the modern way to configure it.

This guide covers rulesets versus classic branch protection rules, a recommended rule set for production branches, CODEOWNERS done properly, and how to roll rules out across an organization.

Rulesets vs classic branch protection rules

Classic branch protection rules still work, but rulesets are better on almost every axis:

  • Layering. Multiple rulesets can apply to the same branch; the most restrictive rule wins. Classic rules allow only one rule per branch pattern.
  • Organization scope. Organization rulesets target many repositories by name pattern or custom property, so a new repository is protected the moment it is created. Classic rules are per repository.
  • Visibility. Anyone with read access can see which rulesets apply, which makes failed pushes easier to understand.
  • Bypass control. The bypass list names exactly which roles, teams or apps may bypass, and whether they may do so always or only through pull requests.
  • Enforcement states. Active, Disabled, and on Enterprise Cloud Evaluate, which records what would have been blocked without blocking it. Evaluate mode is the safest way to roll out new rules.
  • Push rules. Push rulesets can block file paths, file extensions and file sizes across the whole repository network, which branch rules cannot.

Rulesets and classic rules can coexist; when both apply, both are enforced. Migrate gradually, and delete the classic rule once the ruleset covering the same branch is active.

A production branch ruleset

For the default branch of a repository that deploys to production, start with:

RuleSetting
Restrict deletionsOn
Block force pushesOn
Require a pull request before mergingOn
Required approvals1 or 2
Dismiss stale approvals when new commits are pushedOn
Require review from Code OwnersOn
Require approval of the most recent reviewable pushOn
Require conversation resolutionOn
Require status checks to passYour CI jobs, with "require branches to be up to date" if your merge rate allows
Require signed commitsOptional, see below
Require linear historyOptional, team preference

Require approval of the most recent reviewable push deserves attention. Without it, a contributor can get an approval, push a new commit, and merge before anyone reviews the change if stale reviews are not dismissed. With it, the last push must be approved by someone other than its author.

Create it with the API

Rulesets are JSON documents, which makes them easy to version and apply:

shell
cat > main-ruleset.json <<'JSON'
{
  "name": "protect-default-branch",
  "target": "branch",
  "enforcement": "active",
  "conditions": {
    "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] }
  },
  "bypass_actors": [
    { "actor_id": 5, "actor_type": "RepositoryRole", "bypass_mode": "pull_request" }
  ],
  "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": true,
        "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": "build" }, { "context": "test" } ]
      }
    }
  ]
}
JSON

gh api -X POST "/repos/ACME/payments-api/rulesets" --input main-ruleset.json

actor_id: 5 with RepositoryRole is the built-in admin role. With bypass_mode: pull_request, admins can bypass only through a pull request, never with a direct push, so emergency merges still leave a reviewable record. Keep the bypass list short; every entry is a way around the rules.

For status checks, pin each required check to the GitHub App that reports it (the integration_id parameter) when possible. Otherwise any workflow or app that can set a commit status with the same name can satisfy the check.

Organization rulesets

At the organization level (Settings > Rules > Rulesets), the same document gains a repository_name or repository_property condition:

json
"conditions": {
  "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] },
  "repository_property": {
    "include": [ { "name": "tier", "property_values": ["production"] } ],
    "exclude": []
  }
}

Custom properties (for example tier: production) let you protect repositories by classification rather than by name, and repository admins cannot remove an organization ruleset. Roll out with Evaluate where available, read the rule insights for a week, then switch to Active.

Signed commits

Require signed commits ensures every commit on the branch carries a verified GPG, SSH or S/MIME signature. It proves that the commit was signed by a key registered to a GitHub account, which defends against commits that impersonate another author's name and email. It does not prove the code is safe, and it adds friction: every developer needs signing configured, and some bots and merge methods need attention. Commits created through the web UI and by GitHub's merge are signed by GitHub.

Enable it on repositories where author impersonation matters, and set up SSH signing for developers to keep friction low:

shell
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true

Then add the same public key as a signing key in the GitHub account settings.

CODEOWNERS that actually protect something

A CODEOWNERS file maps paths to the people or teams who must review changes to them. GitHub looks for it in .github/, the repository root, then docs/, and uses the first one it finds on the base branch of the pull request.

text
# Default owners for everything
*                           @ACME/platform-reviewers

# Infrastructure and deployment
/infra/                     @ACME/infra
/.github/workflows/         @ACME/platform-security
/.github/CODEOWNERS         @ACME/platform-security

# Payments code
/src/payments/              @ACME/payments

Rules to get right:

  • The last matching pattern wins. Put the catch-all first and specific paths after it. A broad rule at the end silently overrides everything above it.
  • Owners need write access. A team without write permission on the repository cannot satisfy a code owner review, and the file shows an error.
  • Protect the file itself. If anyone can edit CODEOWNERS in their own pull request, they can make themselves the owner of what they are changing. Owning /.github/CODEOWNERS with a security team closes that gap.
  • Protect workflows. /.github/workflows/ should always require review from people who understand the Actions security model. A workflow change can exfiltrate every secret the repository holds.
  • Use teams, not individuals. Individuals go on holiday and leave the company.
  • Validate the syntax. Errors show on the file page and through the API:
shell
gh api "/repos/ACME/payments-api/codeowners/errors" --jq '.errors'

CODEOWNERS only enforces anything when the ruleset has Require review from Code Owners turned on. Without it, owners are merely requested as reviewers.

Two-person integrity and its limits

With one required approval, code owners, last-push approval and stale review dismissal, no single account can get code onto the default branch alone. That is the property you want. A few gaps remain:

  • Admins on the bypass list can still merge alone. Limit bypass to pull requests and alert on repository_ruleset bypass events in the audit log.
  • Other branches that deploy (release branches, deploy/*) need the same rules. Use a refs/heads/release/** include pattern.
  • Tags can trigger release workflows. Add a tag ruleset that restricts creation, update and deletion of v* tags to the release team.
  • Workflows triggered on other branches may still access deployment secrets unless those secrets live in an environment restricted to protected branches.

Push rulesets

Push rulesets block content anywhere in the repository and its fork network, regardless of branch. Useful rules include restricting file paths such as **/*.pem or .env, blocking file extensions such as .exe and .p12, and capping file size. They complement secret scanning push protection, which detects secrets by pattern rather than by file name. Push rulesets are available for private and internal repositories.

Verify the result

Check what applies to a branch before announcing the change:

shell
gh api "/repos/ACME/payments-api/rules/branches/main" \
  --jq '.[] | [.type, .ruleset_source, .ruleset_id] | @tsv'

Then try it: open a pull request that edits a workflow without the security team's review and confirm that the merge button stays blocked. OpenSSF Scorecard's Branch-Protection and Code-Review checks give an outside view of the same settings; see the organization audit guide.

References

Tags:repositoriessupply-chain

Written by

Florian Amette, Security Engineer & Editor

Security engineer writing practical hardening guides for GitHub organizations, repositories and CI/CD pipelines, with a focus on software supply-chain security.