Sub topic 5
Choosing rules: restrictions, history and signatures
This chapter assumes you know how a ruleset is built from targets, rules, bypass actors and an enforcement status. It also assumes you can read JSON. Here you pick the rules that control who can create, move or delete a ref, what shape the commit history has, and whether commits must be signed. A ref is a branch or tag name. Pull request and status check rules have their own chapter.
Three groups of rules
Rules without parameters are listed in JSON as a single type. They fall into three groups.
| Group | Rule type | What it blocks |
|---|---|---|
| Restrictions | `creation` | Creating a ref that matches the target |
| Restrictions | `update` | Any push that moves a matching ref |
| Restrictions | `deletion` | Deleting a matching ref |
| History | `non_fast_forward` | Force pushes, which rewrite history |
| History | `required_linear_history` | Merge commits on matching refs |
| Signatures | `required_signatures` | Commits without a verified signature |
A restriction rule is a gate, not a check. Once creation, update or deletion is active, only actors on the bypass list can do that action. Everyone else is refused.
Restrictions
Use deletion on almost every ruleset. It costs nothing and stops someone removing the default branch or a release tag.
Use update with care. On a branch it blocks every push, including pushes from merged pull requests, unless the actor can bypass. It suits tags and release branches that nobody should move. It is a poor fit for a busy default branch, where you would use the pull_request rule instead.
Use creation to reserve a naming space. For example, you can let only release managers create release/* branches.
History
non_fast_forward blocks force pushes. A force push replaces the commits on a ref with a different set, so audit trails and other people's clones break. Pair it with deletion, because deleting a ref and recreating it is another way to rewrite history.
required_linear_history rejects merge commits, so the branch keeps a straight line of commits. Check the repository settings before you enable it. The repository must still allow squash merging or rebase merging, or nobody can merge a pull request.
Signatures
required_signatures accepts only commits that GitHub marks as verified. A commit is verified when it carries a GPG, SSH or S/MIME signature that matches a key on the author's account. Commits that GitHub creates through the web interface are signed by GitHub and also pass.
Existing unsigned commits are not a problem if they were already on the branch. The rule checks commits that arrive in a push. A pull request with an unsigned commit fails to merge unless the merge method rewrites it, as squash does.
Example: protecting release tags
This organisation ruleset locks every tag that starts with v. Nobody can move it, delete it, or add a tag that carries an unsigned commit. Organisation owners can still bypass it.
{
"name": "protect-release-tags",
"target": "tag",
"enforcement": "active",
"bypass_actors": [
{
"actor_id": 1,
"actor_type": "OrganizationAdmin",
"bypass_mode": "always"
}
],
"conditions": {
"ref_name": {
"include": ["refs/tags/v*"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": []
}
},
"rules": [
{ "type": "update" },
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "required_signatures" }
]
}Common mistakes
- Enabling
updateon the default branch and then wondering why no pull request can merge. - Turning on
required_linear_historywhile the repository allows only merge commits. - Requiring signatures before contributors have set up signing keys. Test with
evaluateenforcement first. - Adding
non_fast_forwardwithoutdeletion, which leaves a way around it.
Exercise
A team wants release branches named release/* to be created only by release managers. They must never be force pushed or deleted. Which rule types do you add, and who needs a bypass entry?
Show answer
Add creation, nonfastforward and deletion. Creation is a restriction, so the release managers must be bypass actors (for example a team) or they cannot create the branches either. Do not add update unless you also want to block normal pushes to those branches.