Collaborators and Branch Protection
Working with others? Invite them as collaborators — and protect main so nothing reaches it without a review.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Nov 25, 2026
Learning objectives#
- Invite collaborators to a personal repository
- Understand collaborator permissions
- Create a branch protection rule
- Require reviews and passing checks
Explanation#
Settings → Collaborators → Add people. On a personal repository collaborators can push, open and merge PRs. Organisations offer finer roles (Read, Triage, Write, Maintain, Admin).
Settings → Branches → Add branch protection rule (or a ruleset under Settings → Rules) for main: require a pull request before merging, require at least one approval, and require status checks (your CI) to pass.
With protection on, a direct git push to main is rejected — every change arrives through a reviewed PR. Admins can be included so the rules apply to everyone.
Step-by-step screens#
Drawings of github.com, the Git installer, VS Code and a terminal. GitHub changes its layout from time to time; the button names stay the same. The red numbers match the numbered actions under each picture; names on your computer will differ.
Loading pictures…
Code example: A sensible protection rule for main#
Branch name pattern: main
☑ Require a pull request before merging
☑ Require approvals: 1
☑ Dismiss stale pull request approvals when new commits are pushed
☑ Require status checks to pass before merging
☑ ci / build-and-test
☑ Do not allow bypassing the above settings
☐ Allow force pushes
☐ Allow deletions
git push origin main → “GH006: Protected branch update failed”. Changes now go through PRs with one approval and green CI.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The rule applies to the branch named main.
- PRs are required, with one approval.
- New commits after approval need a fresh review.
- CI must pass.
- Nobody can bypass, force-push or delete main.
Real-world use case#
In a four-person college project, protection on main ended the “someone pushed broken code the night before the demo” problem.
Common mistakes#
Admins can change or bypass the rules. Give the least access needed.
If the named CI job doesn’t exist, PRs wait forever. Pick checks from the list GitHub shows after CI has run once.
Best practices#
- Protect main on every shared repository
- Require one approval and CI
- Disallow force pushes
- Review access when people leave
Remember this#
Settings → Collaborators.
Settings → Branches → protection rule.
PR + approval + green checks.
Practice exercise#
Your task: Name the two options you would always tick for main in a team.
Show solution
Require a pull request before merging (with 1 approval)
Require status checks to pass before merging
Mini quiz#
With branch protection on main, what happens to git push origin main?
Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions
My notes#
Saved in this browser.