Git Best Practices for Developers

Chapter 2 · Practice 4 of 4

Protect the main branch and require checks before merging

From Branches and pull requests

Why it matters

Rules on the server stop the most common accidents, such as pushing straight to main or rewriting its history, in a way a team agreement cannot.

Turn on branch protection or rulesets for main on your Git host and require:

  • Pull requests before merging
  • At least one approving review
  • Passing CI checks

And block:

  • Force pushes
  • Deletion

These rules also make the main branch trustworthy: anyone can branch from it or deploy it knowing it passed tests. Apply the same protection to release branches if you use them.

Avoid

  • Allowing force pushes to main or release branches