Git & GitHub · Collaboration

Collaborators and Branch Protection

Working with others? Invite them as collaborators — and protect main so nothing reaches it without a review.

Intermediate 5 min Lesson 15 of 19Shows Git 2.47 and GitHub.com as of 2026. GitHub changes its layout from time to time; button names stay the same.
Before you start:Pull Requests
0/19 in Git & GitHub
On this page · 5 min · Intermediate

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#

TextA sensible protection rule for main Illustrative
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
Expected output
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#

  1. The rule applies to the branch named main.
  2. PRs are required, with one approval.
  3. New commits after approval need a fresh review.
  4. CI must pass.
  5. 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#

Giving everyone admin

Admins can change or bypass the rules. Give the least access needed.

Requiring a check that never runs

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
Text Illustrative
Require a pull request before merging (with 1 approval)
Require status checks to pass before merging

Mini quiz#

Collaborators and Branch Protection quizMultiple choice Question 1 of 1

With branch protection on main, what happens to git push origin main?

Score so far: 0/0

Want more? Practise all Git & GitHub MCQs · Git & GitHub interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.