GitHub Rulesets is the next-generation alternative to branch protection rules. It provides more flexible ways to control push, merge, and delete operations for branches and tags.
| Feature | Rulesets | Legacy Protection Rules |
|---|---|---|
| Scope | Repository, Organization, Enterprise | Repository only |
| Conditions | Multiple condition combinations | Branch name matching only |
| Nested Rules | Supported | Not supported |
| Priority | Supported | Not supported |
| Bypass Permissions | More flexible | Simpler |
- Go to repository Settings → Rules → Rulesets
- Click New ruleset → New branch ruleset
- Configure the rules
gh api repos/{owner}/{repo}/rulesets \
--method POST \
-f name='Main Branch Protection' \
-f target='branch' \
-f enforcement='active' \
-f conditions='{"ref_name":{"include":["refs/heads/main"],"exclude":[]}}' \
-f rules='[{"type":"pull_request","parameters":{"required_approving_review_count":1,"dismiss_stale_reviews_on_push":true,"require_last_push_approval":false}}]'{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 2,
"dismiss_stale_reviews_on_push": true,
"require_last_push_approval": false,
"require_review_thread_resolution": true
}
}{
"type": "required_status_checks",
"parameters": {
"required_status_checks": [
{"context": "ci/build"},
{"context": "ci/test"}
],
"strict_required_status_checks_policy": true
}
}{
"type": "non_fast_forward"
}{
"type": "required_linear_history"
}{
"type": "file_parameters",
"parameters": {
"max_file_size": 10240
}
}- Go to organization Settings → Repository → Rulesets
- Click New ruleset → New branch ruleset
- Select the scope
Repositories can inherit organization-level Rulesets:
- Go to repository Settings → Rules → Rulesets
- Check if organization-level Rulesets are already applied
- Repository-level Rulesets will override organization-level rules
name: Release Branch
target: branch
enforcement: active
conditions:
ref_name:
include:
- "refs/heads/release/*"
- "refs/heads/main"
exclude: []
rules:
- type: pull_request
parameters:
required_approving_review_count: 2
- type: required_status_checks
parameters:
required_status_checks:
- context: "ci/build"
- context: "ci/test"
- type: non_fast_forward# Create Ruleset collection
gh api orgs/{org}/rulesets \
--method POST \
-f name='Default Branch Rules' \
-f target='branch' \
-f enforcement='active' \
-f conditions='{"ref_name":{"include":["refs/heads/main"]}}' \
-f rules='[{"type":"pull_request","parameters":{"required_approving_review_count":1}}]'# Repository level
gh api repos/{owner}/{repo}/rulesets
# Organization level
gh api orgs/{org}/rulesetsgh api repos/{owner}/{repo}/rulesets/{ruleset_id} \
--method PATCH \
-f enforcement='disabled'gh api repos/{owner}/{repo}/rulesets/{ruleset_id} \
--method DELETE- Start Simple: Begin with basic protection rules and gradually add complex ones
- Use Organization-Level Rules: Standardize your team's branch management strategy
- Document Rules: Explain your branch strategy in the README
- Review Regularly: Check if the rules are still applicable
- Use Condition Combinations: Apply different rules to different branches
A: A Ruleset is a single rule set, while a Ruleset collection is a combination of multiple Rulesets that can be applied by priority.
A: You need to configure bypass options. Usually only repository administrators or designated users can bypass.
A: Yes, Rulesets will immediately apply to existing branches that meet the conditions.
Previous: GitHub Projects Project Management | Next: Fork and Open Source Contribution