Sub topic 5
Choosing rules: restrictions, history and signatures
This chapter covers the rules in a ruleset that control what can happen to a ref (a branch or tag) and what its commits must look like. It assumes you know basic Git branching, can read a JSON ruleset body, and have read the earlier chapters on ruleset scopes and ref targeting. The pull request, status check and workflow rules are covered in the next two chapters.
The rules in this chapter
Each rule is an entry in the rules array of a ruleset, identified by a type. The rules in this chapter take no parameters, so each is a single line of JSON.
| Rule in the UI | `type` in the API | What it does |
|---|---|---|
| Restrict creations | `creation` | Only bypass actors can create a ref that matches the targets. |
| Restrict updates | `update` | Only bypass actors can push to a matching ref. |
| Restrict deletions | `deletion` | Nobody except bypass actors can delete a matching ref. |
| Require linear history | `required_linear_history` | Rejects merge commits on the ref. |
| Require signed commits | `required_signatures` | Rejects commits without a verified signature. |
| Block force pushes | `non_fast_forward` | Rejects any push that is not a fast-forward. |
The names in the API are not always intuitive. "Block force pushes" is non_fast_forward because a force push is a push that moves a ref to a commit that does not descend from the current one. A ruleset that blocks non-fast-forward updates therefore blocks force pushes.
The diagram is a simplification. GitHub evaluates all active rules and reports every failure together, so a developer sees all the reasons a push was rejected in one message.
Restricting creation, update and deletion
These three rules control the lifecycle of a ref.
Restrict deletions
Deleting a default branch or a release branch is rarely intentional. A deletion rule is cheap and has almost no downside. Rulesets enable it by default in the UI, and the example ruleset in this tutorial includes it.
For tags, deletion matters even more. A deleted and re-created tag can silently point a release name at different code.
Restrict creations
A creation rule stops anyone without bypass from creating a matching ref. Use it where the name of a ref carries meaning. For example, the pattern refs/heads/release/**/* can mean that only release managers create release branches. Pair it with a bypass actor for the release team, not with an open exception.
Restrict updates
An update rule is much stricter than it looks. It blocks every push to the ref, including the merge of a pull request. If you apply it to a branch without bypass actors, nobody can merge into that branch. That is the right choice for a frozen release branch. It is the wrong choice for a normal development branch, where you want changes to arrive through reviewed pull requests.
For ordinary branches, use the pull_request rule instead. It forces changes through a pull request and still allows the merge.
Block force pushes
A force push rewrites history. On a shared branch that can destroy other people's commits, or remove a commit that a release was built from. The non_fast_forward rule rejects those pushes.
This rule applies to the ref itself, not to the pull request branches that feed it. Developers can still force-push their own feature branches unless a ruleset targets those too. If you want to forbid rebasing a feature branch after review begins, target it explicitly, and expect complaints.
Require linear history
A linear history has no merge commits. The required_linear_history rule rejects any push to the ref that contains one, so changes must arrive by squash merge or rebase merge, or by a fast-forward push from a bypass actor.
Teams choose this for three reasons:
git logreads as a single timeline.git bisectworks without stepping through merge parents.- Reverting a change is a single
git revertof a single commit.
The cost is that you cannot preserve the individual commits of a feature branch with a merge commit. If your team values that, leave this rule off and consider requiring squash merges through the repository's merge button settings.
When you enable this rule, also make sure the repository still permits squash or rebase merging in its settings. If only "Create a merge commit" is allowed, no pull request can merge.
Require signed commits
The required_signatures rule rejects pushes that include a commit without a verified signature. "Verified" is GitHub's status, not just the presence of a signature. The signing key must belong to the committer and be registered on their account, or the signature must come from GitHub itself.
Points that cause trouble in practice:
- Every commit in the push is checked, not just the tip. One unsigned commit in an old range of a rebased branch fails the whole push.
- Merges made in the web interface are signed by GitHub, so squash merges from the merge button pass.
- Rebase merges from the web interface may be blocked. The rebased commits are new commits, and GitHub cannot sign them with the author's key. If you need this rule and rebase merging, test it in
evaluatemode first. - Bots and automation need signing too. A workflow that pushes with
GITHUB_TOKENandgit commitwill produce unsigned commits. Give such workflows a signing key, use the API to create commits (GitHub signs those made with an app token), or add the automation as a bypass actor.
To check what developers will experience, they can run git log --show-signature locally, or look for the Verified badge on a commit on GitHub.
Putting the rules together
The following ruleset applies to the default branch of every repository in an organisation that is not named sandbox-*. It blocks deletion and force pushes, requires linear history, and requires signed commits. It has no pull_request rule, so you can combine it with the earlier example ruleset.
{
"name": "history-and-signatures",
"target": "branch",
"enforcement": "evaluate",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "required_linear_history" },
{ "type": "required_signatures" }
]
}It starts with "enforcement": "evaluate" so the rules are recorded but not enforced. You can review which pushes would have failed in the rule insights before switching to active. A later chapter covers that process in detail.
A second ruleset protects release tags. Tag rulesets use "target": "tag", and the ref_name patterns use the full refs/tags/ prefix.
{
"name": "protect-release-tags",
"target": "tag",
"enforcement": "active",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["refs/tags/v*"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": []
}
},
"rules": [
{ "type": "update" },
{ "type": "deletion" },
{ "type": "non_fast_forward" }
]
}Here update is the right rule. Once a version tag exists, it should never move. Because creation is not restricted, anyone can still publish a new tag such as v2.4.0, but nobody can change or delete an existing one.
You can create either ruleset with the same call used earlier:
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets \
--input protect-release-tags.jsonChoosing rules: a decision guide
Match each rule to the failure it prevents, and add only what you can justify.
- Default branch of every repository:
deletion,non_fast_forward, and a pull request rule. Add linear history if your team already squashes. - Release branches: the same, plus
creationrestricted to release managers. Considerrequired_signatures. - Release tags:
update,deletion,non_fast_forward. - Frozen branches during an incident or code freeze: temporarily add
updateand remove it afterwards. - Feature branches: usually no ruleset. Restrictions here slow people down for little gain.
Each extra rule is a place where a legitimate push can fail, so test with evaluate enforcement and name the ruleset after its purpose so error messages are readable.
Common mistakes
- Using
updatewhere you meantpull_request. This blocks merges for everyone. - Forgetting that rules do not stack silently with merge settings. Linear history plus "merge commits only" in repository settings means nothing can merge.
- Assuming signed commits cover automation. Workflows that push commits fail until you sign them or add a bypass actor.
- Targeting only branches. Tags are separate refs with a separate
target, so a branch ruleset does not protect them.
Exercise
Your organisation wants every repository's default branch to forbid deletion, force pushes and merge commits. Developers still need to merge pull requests with the merge button. Which rule types go in the ruleset, and which rule would you leave out to avoid blocking merges?
Show answer
Use deletion, non_fast_forward and required_linear_history. Leave out update, because it blocks every push to the ref, including pull request merges, for anyone who is not a bypass actor. Also check that squash or rebase merging is enabled in each repository's settings, because linear history rejects merge commits.
Exercise
A scheduled workflow commits a generated changelog to the default branch with git commit and git push. After you enable required_signatures, the workflow fails. Give two ways to fix it.
Show answer
Either sign the workflow's commits (for example by giving the workflow a signing key registered to a bot account, or by creating the commit through the API with a GitHub App token so GitHub signs it), or add the workflow's app or role as a bypass actor so its pushes skip the rule. Prefer signing, because it keeps the rule in force for the automation as well.