GitHub Branch Protection Rules
Protecting production branches: requiring pull request reviews, requiring status checks (CI), preventing force pushes, and repository governance.
GitHub Branch Protection Rules
Branch Protection Rules (and GitHub Repository Rulesets) safeguard your critical branches (such as main and release/*) against accidental deletion, unreviewed code pushes, and failing builds.
1. Essential Protection Settings for main
Navigate to Settings → Branches (or Rulesets) and configure the following rules for main:
1. Require a Pull Request Before Merging
- Disallows anyone (including admins) from pushing directly to
mainviagit push origin main. - Require approvals: Set to at least 1 or 2 approving reviews.
- Dismiss stale pull request approvals: Automatically dismisses approvals whenever new commits are pushed to the PR.
2. Require Status Checks to Pass Before Merging
- Blocks merging until automated CI test suites (e.g.
pnpm validate-content,pnpm test,next build) succeed. - Require branches to be up to date before merging: Ensures the PR was tested against the latest tip of
main.
3. Block Force Pushes & Deletions
- Completely disables
git push --forceonmain, preventing accidental destruction of repository history.
2. CODEOWNERS File
You can automatically assign specific team members or engineers as required reviewers when files in specific folders are modified:
# .github/CODEOWNERS
# Global default owners:
* @iamtashanto
# Backend database changes require backend lead review:
/prisma/ @iamtashanto/backend-team
# Documentation changes require docs team review:
/content/ @iamtashanto/docs-teamRelated Topics
Effective Code Reviews on GitHub
Conducting constructive code reviews: inline diff comments, suggestion blocks, review states (Approve, Request Changes), and review etiquette.
GitHub CLI (gh) Command Reference
Mastering the official GitHub command line tool: gh auth, gh repo clone, gh pr create, gh issue list, and gh workflow run.