AI Hints
CI/CD with GitHub Actions · Lesson 70 of 138

Branch Protection

Guarding main.

⏱ 2 min read▶ Video at 1:20:12🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

CI can tell you a change is broken, but it can't stop someone from merging it anyway. Branch protection can. It's a set of rules on the main branch, that GitHub enforces for everyone.

Branch Protection
From the video: Branch Protection
  • CI reports, but can't block
  • Branch protection can
  • Rules for everyone

Common rules

In the repository settings, under rules, you protect main. Require a pull request, so nobody pushes directly. Require approvals from reviewers. Require status checks to pass, which is where CI comes in. And block force pushes, so history can't be rewritten.

keyvalue
Require a PRno direct pushes
Approvals1 or more reviewers
Status checksCI must be green
No force pushhistory is safe

A protected merge

With protection on, the only way into main is a pull request. CI runs, and must pass. A reviewer must approve. Only then is the merge button enabled. If someone pushes a new commit, the checks run again on the new code.

  1. Pull request
  2. CI passes
  3. Review approved
  4. Merge enabled
A protected merge
From the video: A protected merge

Summary

Branch protection makes the rules real. Pull requests, approvals, and green checks, before anything reaches main. Next, choosing which checks are required.

Key takeaways
  • Protect main
  • Require PRs
  • Require green checks
Next up: Required Checks — which jobs must pass