Sub topic 1
Why protect branches
This chapter explains the risks that branch protection reduces and introduces the two mechanisms GitHub offers. Later chapters configure each one.
Prerequisites
This chapter assumes you have:
- Basic Git use: branches, commits, merges, and pushes.
- Familiarity with GitHub pull requests.
- A basic idea of continuous integration (CI) and status checks.
- Admin or owner access to a repository you can experiment in.
What goes wrong on an unprotected branch
By default, anyone with write access can push straight to any branch, including main. That allows several kinds of damage:
- Unreviewed changes. A commit pushed directly to
mainskips code review, so a mistake or a malicious change reaches production code without a second pair of eyes. - Broken builds. Code that fails tests can land on the branch everyone else builds from.
- Rewritten history. A force push replaces the branch's commits. Work disappears, and collaborators' clones no longer match the remote.
- Deleted branches. Deleting a release branch or tag removes the reference that deployments or users depend on.
- Unverified authorship. Without signed commits, a commit can claim to come from someone who did not write it.
Most of these are accidents, not attacks. Protection rules matter because they make the safe path the only path, instead of relying on every contributor to remember the process.
What a protection rule does
A protection rule is a server-side check. GitHub evaluates it when someone pushes to a branch or merges a pull request, and rejects the action if it breaks the rule. Because the check runs on GitHub, it applies no matter which client or local Git configuration the contributor uses.
Typical rules fall into four groups:
| Goal | Example rule |
|---|---|
| Review | Require a pull request with one approval |
| Quality | Require the `build` status check to pass |
| History | Block force pushes and deletions |
| Ownership | Require review from code owners |
Two mechanisms
GitHub has two systems for these rules:
- Classic branch protection rules. Each rule matches a branch name pattern and applies a set of settings. This is the older mechanism.
- Rulesets. The newer mechanism. A ruleset can target branches or tags, can be tested in an evaluate mode before it blocks anything, supports a bypass list, and can apply across an organization.
Both can apply to the same branch at once. This tutorial covers classic rules first, then rulesets, then how they combine and how to choose between them.
Which branches to protect
Start with the branches that other work depends on:
- The default branch, usually
main. - Release branches that you maintain, such as
release/*. - Tags that mark releases, which rulesets can protect.
Feature branches rarely need protection. Over-protecting them slows contributors without lowering risk.
Exercise
A contributor runs git push --force origin main from a local clone with a custom Git configuration. Why can a protection rule still stop this?
Show answer
Protection rules are enforced by GitHub on the server when the push arrives, so they apply regardless of the client or its local configuration. A rule that blocks force pushes rejects the push.