Sub topic 2
Classic branch protection rules and name patterns
This chapter assumes you know basic Git (branches, commits, merges, pushes), have used GitHub pull requests, and have admin access to a repository you can experiment in.
What a classic rule is
A branch protection rule is a set of restrictions that GitHub applies to every branch whose name matches a pattern. The rule lives in the repository settings. Each rule has one pattern and a list of switches, such as "require a pull request" or "block force pushes". Rulesets are the newer system and come in later chapters. Classic rules are still widely used, and you will meet them in existing repositories.
Name patterns
The pattern decides which branches the rule covers. It can be an exact name or a wildcard pattern.
| Pattern | Matches |
|---|---|
| `main` | Only the branch named `main` |
| `release/*` | `release/1.0` and `release/2.x`, but not `release/1.0/hotfix` |
| `release/**/*` | Any branch under `release/`, at any depth |
| `*` | Any branch name without a slash |
The * wildcard does not cross a / boundary. Use ** when you need to match across slashes.
If a branch matches more than one rule, the settings from all matching rules apply together. When two rules set the same option differently, the stricter setting wins.
A rule can also match a branch that doesn't exist yet. Once someone creates a branch with a matching name, the rule applies to it.
Creating a rule
- Open the repository, then Settings and Branches.
- Under branch protection rules, choose Add rule.
- Enter a branch name pattern, for example
main. - Turn on the protections you want and save.
The following chapters cover the individual protections. At this stage you only need to know how a rule is built and how it is attached to branches.
Setting a rule through the API
A rule can also be created from a JSON body. This body sets the pull request, status check, and history options on one branch.
{
"required_status_checks": {
"strict": true,
"checks": [{ "context": "build" }]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true,
"require_code_owner_reviews": true
},
"restrictions": null,
"allow_force_pushes": false,
"allow_deletions": false
}Save it as protection.json and send it with the gh CLI.
gh api --method PUT repos/OWNER/REPO/branches/main/protection --input protection.jsonReplace OWNER and REPO with your own values. The endpoint is PUT, so it replaces the whole rule for that branch. Any option you leave out goes back to its default, so always send the complete body.
Common errors
- A pattern that is too narrow. A rule for
release/*does not protectrelease/1.0/hotfix. Check the branch name against the table above. - Assuming the rule exists before the branch does. A rule on
mainhas no effect if the default branch is calledmaster. - Partial updates. Sending only one option through the API resets the others.
- Expecting a rule to bind everyone. Administrators can bypass a rule unless the admin option is on. A later chapter covers this.
Exercise
A repository has a rule for the pattern release/*. Does it protect the branch release/2.0/hotfix? What pattern would cover it?
Show answer
No. The * wildcard does not cross a /, so release/* matches release/2.0 but not release/2.0/hotfix. The pattern release/**/* matches branches at any depth under release/, including this one.
Exercise
You send a PUT request to the branch protection endpoint containing only allow_force_pushes: false. What happens to the other options on that rule?
Show answer
They go back to their defaults, because PUT replaces the whole rule. You should send the complete JSON body every time.