Class 44 of 50 • DevOps & CI/CD

GitHub Branch Protection Rules: Guarding Production & CODEOWNERS

In an organization with 50 engineers, all it takes is one sleep-deprived developer accidentally running git push origin main --force to overwrite the entire production codebase. GitHub Branch Protection Rules act as cryptographic armor around your mission-critical branches.

Setting Up Branch Protection Rules

  1. Navigate to your repository → Settings → Branches (or Rules → Rulesets).
  2. Under Branch protection rules, click Add rule.
  3. Under Branch name pattern, enter: main (or master, release/*).

The 4 Essential Production Protection Gates

1. Require a Pull Request Before Merging

Disables direct pushes to main! Any changes must come through a Pull Request:

  • Require approvals: Set to at least 1 or 2 approving reviews from teammates before the merge button becomes active.
  • Dismiss stale pull request approvals when new commits are pushed: If a developer gets an approval and then sneaks in a new commit, GitHub automatically revokes the approval and demands a fresh review!

2. Require Status Checks to Pass Before Merging

Ensures that code cannot be merged if automated CI/CD pipelines fail:

  • Check "Require status checks to pass before merging".
  • Search for your GitHub Actions job names (e.g. build-and-test, lint) and mark them as Required.
  • Check "Require branches to be up to date before merging" to ensure the PR has tested against the latest code on main.

3. Require Conversation Resolution Before Merging

Ensures that all inline comments and critique left by reviewers have been explicitly marked as resolved before merging is allowed.

4. Do Not Allow Force Pushes & Deletions

Completely disables git push --force and prevents anyone from deleting the main branch!

Automating Reviews with a CODEOWNERS File

In large codebases, different engineering teams own different folders (e.g., Security owns /auth, Frontend owns /views). Create a file at .github/CODEOWNERS:

# .github/CODEOWNERS

# Default fallback owners for everything in repository:
*       @core-architects

# Security team must review any changes to authentication:
/src/auth/          @security-team
/src/crypto/        @security-team
package.json        @lead-architect @security-team

# Frontend team owns UI assets:
/src/components/    @frontend-team
*.css               @frontend-team

When any PR modifies files matching these patterns, GitHub automatically assigns the designated code owners for review!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Pull Requests & Peer Reviews

Enforce required peer approvals before any developer can merge into main.

PR Merge Types & History Cleanliness

Configure allowable merge types in GitHub repository settings.

Automated Status Checks

Require passing CI/CD pipelines before enabling the PR merge button.

Enterprise Branching Governance

Implement security and compliance standards for regulated software projects.

Frequently Asked Questions (FAQ) - Student & Interview Review

By default, admins can bypass rules. However, checking the option 'Do not allow bypassing the above settings' enforces the rules strictly on EVERYONE, including repository owners and administrators.
GitHub Rulesets (introduced in 2023) allow you to apply rules across multiple repositories simultaneously, target branches using regular expressions, and test rules in 'Evaluate' mode before hard enforcement.
The push is immediately rejected by GitHub's server with: `remote: error: GH006: Protected branch update failed for refs/heads/main. At least 1 approving review is required.`

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Git Branching Strategies → Previous: ← CI/CD Pipelines | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!