Secure Coding

Protecting Your Main Branch With Review Rules

Learn how branch protection and mandatory code review keep your main Git branch stable, and how to set rules that help a team without slowing it down.

On this page
  1. Why the main branch needs special treatment
  2. The core rules worth enabling
  3. Designing a review process that people will follow
  4. Handling emergencies without breaking the rules
  5. Common mistakes to avoid
  6. Frequently Asked Questions

Every Git project has one branch that everything depends on. It is usually called main, and it is the branch that gets built, tested and shipped. If a bad change lands there, the damage spreads fast: broken builds, failed deployments, or worse, a vulnerability that goes live before anyone notices.

Branch protection rules are the simplest way to guard that branch. They do not require special tools or a large security budget. They require a few settings and a team that agrees on how changes should move from an idea to production. This guide explains what those rules are, why they matter, and how to choose a sensible set.

Why the main branch needs special treatment

A Git repository lets anyone with write access push commits to any branch. That freedom is useful for feature work, but it is risky for the branch that represents your released code. Three problems show up again and again.

  • Accidental damage. A force push can rewrite history and erase commits. A rushed fix can skip tests and break production.
  • Unreviewed code. When one person writes and merges a change alone, nobody else has a chance to catch logic errors, unsafe input handling or leaked credentials.
  • Compromised accounts. If an attacker steals one developer's credentials, an unprotected main branch lets them push malicious code directly.

Protection rules turn these risks into guarded steps. Changes must arrive through a proposal (often called a pull request or merge request), pass checks, and get approval from another person.

The core rules worth enabling

Most hosting platforms offer similar options under different names. These are the ones that give the most value.

Require a pull request before merging

This blocks direct pushes to the protected branch. Every change, including small ones, travels through a proposal that has a visible discussion and a record of who approved it. That record is useful months later when you need to understand why something changed.

Require at least one approving review

A second pair of eyes catches mistakes the author is too close to see. For most teams, one required approval is the right starting point. Raising it to two makes sense for sensitive areas such as authentication, payments or deployment scripts.

Dismiss stale approvals when new commits arrive

Imagine a reviewer approves a change, and then the author pushes extra commits that alter its behavior. Without this setting, the old approval still counts. Turning it on forces a fresh look whenever the code changes after review.

Require status checks to pass

Automated checks run your test suite, linters and security scanners. Making them mandatory means a change cannot merge while tests are failing. It also removes the awkward social pressure of a reviewer having to say "the build is red, please fix it."

Block force pushes and branch deletion

These two options protect your history. Force pushes rewrite commits that others may have already built upon, and deleting the main branch is almost never what anyone wants.

Require signed commits (optional)

Commit signing proves that a commit really came from the key holder, not just from someone who typed a name into their Git configuration. It adds some setup work for contributors, so adopt it when your threat model calls for it.

Designing a review process that people will follow

Rules that feel like obstacles get worked around. A good review process is strict about the important things and light about the rest.

  1. Keep changes small. A change of a few dozen lines gets a real review. A change of thousands of lines gets a quick glance and a hopeful approval.
  2. Write clear descriptions. The author should explain what changed, why, and how to test it.
  3. Use a checklist for security-sensitive code. Ask whether input is validated, whether secrets are absent, and whether permissions are checked.
  4. Set expectations for response time. If reviews sit for days, people will look for shortcuts.

A file named CODEOWNERS helps here. It maps parts of the repository to the people or teams responsible for them, so a change to the deployment folder automatically asks the right reviewers. The exact syntax varies a little by platform, but the idea is the same everywhere.

# Example CODEOWNERS entries
/deploy/    @platform-team
/auth/      @security-team
*.md        @docs-team

Handling emergencies without breaking the rules

Sooner or later, production breaks at an awkward hour and a fix cannot wait for the usual process. Plan for this ahead of time rather than improvising.

  • Allow a small group of administrators to bypass protection, and log every bypass.
  • Prefer a fast review by one available person over no review at all.
  • After the emergency, open a follow-up proposal to review the change properly and add a test that would have caught the problem.

Teams that skip this planning tend to disable protection "just for today" and forget to turn it back on.

Common mistakes to avoid

  • Exempting administrators from all rules. Administrator accounts are high-value targets. Applying the rules to everyone keeps them honest and limits the damage from a stolen account.
  • **Protecting only main.** Release branches and long-lived maintenance branches deserve the same treatment.
  • Treating approval as a rubber stamp. If reviewers approve without reading, the rule exists on paper only. Encourage comments and questions.
  • Forgetting repository settings themselves. Who can change the protection rules is as important as the rules. Restrict that permission to a small, trusted group.

Frequently Asked Questions

Do small teams or solo developers need branch protection?

Yes, though in a lighter form. A solo developer cannot have a second reviewer, but can still require pull requests and passing status checks. That prevents accidental pushes, creates a clean history, and gives automated tests a chance to catch mistakes before they reach the main branch.

Will required reviews slow my team down?

They add a small delay to each change, but they usually save time overall by catching defects early, when fixing them is cheap. Keeping changes small, setting response-time expectations and using automated checks for routine issues keeps the process quick.

What is the difference between branch protection and code owners?

Branch protection defines the general rules for a branch, such as requiring reviews and passing checks. Code owners define who must do the reviewing for specific files or folders. They work best together: protection enforces that a review happens, and code owners make sure the right people perform it.