Branch protection and rulesets
| Type | Repository access control |
|---|---|
| On GitHub | Branch protection rules (classic), rulesets |
| On GitLab | Protected branches |
| Settings | Settings, then Branches or Rulesets |
| OWASP Top 10 | A03:2025 |
| Related | Securing a GitHub repository |
A Git branch is a name that points at one commit. Anyone with write access to a repository can move that name with a push, and a force push can point it at a different history. Branch protection is a set of conditions that the code host checks before it accepts a change to a named branch: that the change arrived through a reviewed pull request, that required checks passed, that the commits are signed, or that nobody may change the branch at all.1 GitHub offers two systems for this, the older branch protection rules and rulesets, which also cover tags and whole pushes.2
On an unprotected branch, a stolen token with write access or a compromised contributor account can put code straight onto the branch that gets built, which makes branch protection a supply chain control. OpenSSF Scorecard, which scores open-source projects, rates a missing rule as high risk for deliberate injection of malicious code; blocking force pushes and branch deletion make up the first tier of its branch check, worth 3 of 10 points.3
What a rule checks
A classic GitHub rule blocks force pushes to its branches, and their deletion, by default. The rule names its branches with a pattern such as main or *release*, and the branch need not exist yet.14
With required reviews on, a collaborator can change the branch only through a pull request that the set number of people with write permission have approved. An approval records the diff as it stood when the reviewer approved it; Dismiss stale pull request approvals when new commits are pushed withdraws the approval when that diff changes, and Require approval of the most recent reviewable push means someone other than the last person to push has to approve. With neither option on, an approval survives new commits; GitHub calls a pull request that gains unapproved content after approval "hijacked".1
Required status checks hold a merge until named CI jobs report success, a skip or a neutral result. Any person or integration with write permission can set the state of any status check, so a rule can name the app expected to report each one and refuse a result from anyone else.1 Other options require signed commits, a linear history or resolved review comments. Lock branch makes the branch read-only.1 Through a ruleset, a merge can also wait for code scanning: it is blocked while an alert at a set severity is open, while analysis is still running, or when the required tool is not set up.5
Bypass
Repository administrators are not bound by a classic rule unless Do not allow bypassing the above settings is selected, and administrators can always push to a branch even when Restrict who can push to matching branches limits everyone else.1 Named bypass lists exist only in repositories owned by an organization.4
A ruleset carries its own bypass list. It can name repository administrators, organization and enterprise owners, the maintain or write role, teams, GitHub Apps and Dependabot. An actor set to For pull requests only may skip the rules but still has to open a pull request, which leaves the change in the pull request history and the audit log.6 Whoever steals the credentials of a listed account skips the rules too, so the list belongs in any Principle of least privilege review of the repository.
Rulesets
A ruleset is a named list of rules with a target and an enforcement status. A repository can hold up to 75 of them.2 Only one classic rule can apply to a branch, while rulesets have no priority: when several target the same branch, their rules are combined, and where two define the same rule differently, the stricter one wins. Rulesets also combine with a classic rule: three required reviews in a ruleset and two plus a linear history in a classic rule give a branch that needs three reviews and a linear history.2
Switching a ruleset off does not delete it, and anyone with read access can see the active ones.2 The REST API returns every active rule that applies to a branch name, from repository and organization rulesets alike; for a public repository it needs no token:7
curl https://api.github.com/repos/OWNER/REPO/rules/branches/mainTags
A workflow can call an action by a version tag such as v44.5.1, and a tag can be moved to another commit like a branch. A tag ruleset with Restrict updates and Restrict deletions leaves only the bypass list able to move or remove matching tags.5 Between 14 and 15 March 2025, an attacker moved several version tags of the action tj-actions/changed-files, used by more than 23,000 repositories, to a malicious commit that printed CI secrets into workflow logs.8 Workflows guard against a moved tag with pinning to a commit hash.
GitLab
GitLab protects a project's default branch by default. A new rule sets Allowed to push and merge to Maintainers; setting it to No one blocks direct pushes, so changes arrive through merge requests. When a group rule and a project rule match the same branch, GitLab applies the most permissive result for most settings, the opposite of how GitHub combines rulesets.9
A baseline for the default branch
On GitHub, open Settings, then Rulesets under Code, planning, and automation, click New ruleset and New branch ruleset, and set the enforcement status to Active. Under Target branches, include the default branch.6 Keep Restrict deletions and Block force pushes, which are selected already, and add Require status checks to pass before merging with the CI job names. Require a pull request before merging on its own asks only that a pull request be opened, not approved, so it needs a number of required approvals as well.5 One required reviewer lowers the chance that a compromised contributor slips code in, and a second guards against an attacker who also controls a reviewer's account; neither helps when author and reviewer are one person with two accounts.3 What the reviewer looks for is in Security code review.
In SecFlash
SecFlash reports a default branch with no protection rule as SF-CLD-601 on the Cloud page.
See also
- Securing a GitHub repository
- Security code review
- Dependabot
- Software supply chain compromise
- Over-privileged access token
- Pwn request
- Category: Software supply chain
References
- a b c d e f GitHub (2026). "About protected branches". GitHub Docs.
- a b c d GitHub (2026). "About rulesets". GitHub Docs.
- a b OpenSSF. "Check Documentation". OpenSSF Scorecard, GitHub.
- a b GitHub (2026). "Managing a branch protection rule". GitHub Docs.
- a b c GitHub (2026). "Available rules for rulesets". GitHub Docs.
- a b GitHub (2026). "Creating rulesets for a repository". GitHub Docs.
- back GitHub (2026). "REST API endpoints for rules". GitHub Docs.
- back GitHub (15 March 2025, updated 22 October 2025). "tj-actions changed-files through 45.0.7 allows remote attackers to discover secrets by reading actions logs" (GHSA-mrrh-fwg8-r2c3). GitHub Advisory Database.
- back GitLab. "Protected branches". GitLab Docs.