Sub topic 9
Protecting Important Branches
This chapter assumes you can create branches, open pull requests, and push to a remote such as GitHub. It covers how to stop people from changing your main branch in ways your workflow does not allow.
Why protect a branch
A workflow is only a convention until the host enforces it. If your team agrees that every change to main goes through a reviewed pull request, nothing stops someone from running git push origin main directly. Branch protection turns the agreement into a rule. The host rejects any push or merge that breaks it.
Protect the branches that other work depends on: main, develop in Gitflow, and long-lived release/* branches.
Common rules
| Rule | What it does | Why you would use it |
|---|---|---|
| Require a pull request | Blocks direct pushes to the branch | Every change gets a review point |
| Required approvals | Needs N approving reviews before merge | Catches mistakes and spreads knowledge |
| Dismiss stale approvals | Removes approvals when new commits arrive | Stops a reviewed change from being swapped for an unreviewed one |
| Required status checks | Needs named CI jobs to pass | Keeps broken builds out of the branch |
| Require up-to-date branch | Branch must contain the latest base commit before merge | The checks ran against what will actually be merged |
| Linear history | Allows only squash or rebase merges | Keeps history free of merge commits |
| Block force pushes and deletion | Refuses `--force` and branch removal | Protects shared history |
How the rules fit together
The diagram shows the path a change takes to reach a protected branch. Failing any gate sends the author back to the feature branch.
Setting it up with the GitHub CLI
You can configure rules in the repository settings. The GitHub API lets you keep them in a script. This example protects main with one approval and a required CI check named test. Replace OWNER/REPO with your repository.
gh api --method PUT repos/OWNER/REPO/branches/main/protection \
--input - <<'EOF'
{
"required_status_checks": { "strict": true, "contexts": ["test"] },
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true
},
"restrictions": null,
"required_linear_history": true,
"allow_force_pushes": false,
"allow_deletions": false
}
EOFHere "strict": true is the "require up-to-date branch" rule. enforce_admins applies the rules to administrators too. Newer repositories can also use rulesets, which cover several branches by pattern, such as release/*.
Match the rules to the workflow
- GitHub Flow and trunk-based development: require a pull request and passing checks. Keep the required checks fast, because developers wait on them many times a day.
- Gitflow: protect both
mainanddevelop. Addrelease/*once a release branch exists. - Linear history: turn it on only if the team chooses squash or rebase merging. With it on, the merge button offers no merge-commit option.
Common errors
- A required check never reports. The check name in the rule must match the CI job name exactly. A renamed job leaves the pull request waiting forever.
- Rules skipped by admins. Without
enforce_admins, an administrator can push straight tomain. - Too many approvals. Requiring three reviewers on a four-person team stalls every change. Start with one.
- Strict mode on a busy repository. Each merge makes every other open pull request out of date, so each one must be rebased and re-tested. A merge queue handles this by testing changes in order.
Exercise
Your team uses squash merging and wants every merged pull request to be tested against the latest main. Which two protection rules do you enable, and what cost does the second one add?
Show answer
Enable required status checks and the "require branch to be up to date before merging" rule (strict). The cost is that whenever another pull request merges first, your branch is out of date and must be updated and re-tested before it can merge. On a busy repository this causes repeated reruns, which a merge queue can reduce.