Sub topic 2
Choosing rules: restrictions, history and signatures
Assume you know Git branches, pull requests, GitHub Actions, and JSON. A ruleset combines a target, rules, bypass actors, and an enforcement status. This chapter focuses on rules that protect changes before they reach a protected branch.
Restrictions, history, and signatures
Start with rules that prevent irreversible or hard-to-review changes:
- Deletion blocks deleting the targeted branch or tag.
- Non-fast-forward blocks force pushes, which can rewrite published history.
- Required linear history rejects merge commits. Every change must be rebased or squashed into a straight commit sequence.
- Required signatures requires commits to have a verified signature.
Linear history makes tools such as git log easier to follow. Signed commits improve confidence about authorship, but they do not prove that the code is safe. Choose these rules according to your release and audit requirements.
Pull requests, reviews, and status checks
A pull request rule can require review before merging. Useful parameters include:
required_approving_review_count: the number of approvals.dismiss_stale_reviews_on_push: removes approvals after new commits.require_code_owner_review: requires approval from an owner of changed files.required_review_thread_resolution: requires conversations to be resolved.required_status_checks: requires named checks to pass.
A common baseline is one approval, stale-review dismissal, code-owner review, and resolved threads. Add required status checks for tests, linting, security scans, or builds. Do not require checks that do not run reliably on every applicable pull request.
Requiring a shared workflow
A workflows rule can require a workflow file from a central repository to pass. Specify the repository ID, workflow path, and branch reference. The workflow must report a successful result before the pull request can merge. Set do_not_enforce_on_create to false when newly created repositories must follow the rule immediately.
Granting limited bypass access
Use the narrowest bypass actor and mode that supports emergency work. Prefer a team or repository role over all organisation members. Prefer pull_request bypass mode, which keeps an emergency change reviewable, instead of unrestricted direct bypass. Keep bypasses rare, document them, and review their use.
Exercise
Which rules prevent force pushes and require a review before merging?
Show answer
Use the non-fast-forward rule to block force pushes and a pull request rule with at least one required approving review.