AI Hints
Home›Git & GitHub Tutorial›Team Workflows
Git & GitHub · Lesson 79 of 144

Protecting Main

Rules GitHub enforces.

⏱ 2 min read▶ Video at 1:25:58🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Rules work best when nobody can break them by accident. GitHub lets you protect a branch, usually main, with rules it enforces automatically.

Protecting Main
From the video: Protecting Main
  • Rules can be broken by accident
  • GitHub can enforce them
  • Protect the main branch

Common protection rules

In the repository settings, under branches, or rules, you can require a pull request before merging, so nobody pushes straight to main. Require at least one approval. Require automatic checks, like tests, to pass. And block force pushes, so history on main can never be rewritten.

Require a PRno direct pushes
Require approvalat least one reviewer
Require checkstests must pass
Block force pushhistory stays safe

Without and with protection

Without protection, anyone with access can push straight to main, or even force push, and break things for everyone. With protection, GitHub rejects direct pushes to main. Every change has to arrive through a reviewed pull request, with its checks passing. Mistakes get caught before they reach main.

Unprotected main

  • Direct pushes allowed
  • Force pushes allowed
  • Mistakes reach main

Protected main

  • Pull requests only
  • Reviews and checks required
  • Mistakes caught early

Summary

Branch protection makes GitHub enforce your rules. Pull requests, approvals, passing checks, and no force pushes. Next, a few habits that make a team work smoothly.

Key takeaways
  • Settings, branches or rules
  • Require PRs and approvals
  • Block force pushes
Next up: Good Team Habits — names, sizes and messages