Git and GitHub · Part 3 · GitHub and remote repositories
Branch protection and CODEOWNERS
Rules instead of promises
“Never push straight to main” and “never force-push main” are good rules, until someone forgets, late on a Friday. Branch protection makes GitHub enforce them: a push that breaks a rule is rejected, whoever sends it.
The rules teams use
| Rule | What it does |
|---|---|
| Require a pull request before merging | No direct pushes: every change to main comes through a pull request |
| Required approvals | At least one (or more) approving review |
| Dismiss stale approvals | A new push after an approval needs a new approval |
| Require status checks to pass | Tests and builds must be green before merging |
| Require conversation resolution | Every review comment must be resolved |
| Require linear history | Only squash or rebase merges, no merge commits |
| Block force pushes | Nobody can rewrite main's history |
| Restrict deletions | Nobody can delete main |
Protecting main with a ruleset
GitHub has two ways to protect a branch: classic branch protection rules and the newer rulesets. Both enforce the rules above; rulesets can cover many branches at once and be switched off without being deleted. The steps for a ruleset:
1.Open the settings
In the repository, go to Settings, then Rules, then Rulesets, then New ruleset, then New branch ruleset.
2.Name it and choose the branch
Name it Protect main, set Enforcement status to Active, and under Target branches click Add target, then Include default branch.
3.Turn on the rules
Restrict deletions and Block force pushes are on by default. Add Require a pull request before merging with 1 required approval, and, once there are automated tests, Require status checks to pass. Click Create.
4.Try a direct push
git switch main
git commit --allow-empty -m "Test protection"
git pushremote: error: GH013: Repository rule violations found for refs/heads/main.
remote: - Changes must be made through a pull request.
! [remote rejected] main -> main (push declined due to repository rule violations)It works. Undo the test commit with git reset --hard origin/main.
CODEOWNERS
On a bigger codebase, different people know different parts. A CODEOWNERS file says who owns what, and GitHub automatically asks those people to review any pull request that touches their files:
# Everything, unless a later line says otherwise
* @jknithin36
# The design system
/components/ui/ @design-team-lead
# Anything about payments
/lib/payments/ @payments-dev @jknithin36
# Every CSS file
*.css @design-team-leadPatterns work like .gitignore. When several lines match a file, the last one wins, so the catch-all * goes first. Owners need write access to the repository, and teams can be owners too, as @org/team-name.
Turning on Require review from Code Owners in the pull request rule makes their approval required, not just requested.
Quick reference
| Goal | Setting |
|---|---|
| No direct pushes to main | Require a pull request before merging |
| Every change reviewed | Required approvals: 1 or more |
| Nothing broken merged | Require status checks to pass |
| History never rewritten | Block force pushes |
| Experts review their area | .github/CODEOWNERS + Require review from Code Owners |