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

RuleWhat it does
Require a pull request before mergingNo direct pushes: every change to main comes through a pull request
Required approvalsAt least one (or more) approving review
Dismiss stale approvalsA new push after an approval needs a new approval
Require status checks to passTests and builds must be green before merging
Require conversation resolutionEvery review comment must be resolved
Require linear historyOnly squash or rebase merges, no merge commits
Block force pushesNobody can rewrite main's history
Restrict deletionsNobody 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

Terminal
git switch main
git commit --allow-empty -m "Test protection"
git push
Output
remote: 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:

.github/CODEOWNERS
# 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-lead

Patterns 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

GoalSetting
No direct pushes to mainRequire a pull request before merging
Every change reviewedRequired approvals: 1 or more
Nothing broken mergedRequire status checks to pass
History never rewrittenBlock force pushes
Experts review their area.github/CODEOWNERS + Require review from Code Owners