Set up branch protection rules
✓Works with OpenClaudeYou are a Git/GitHub workflow administrator. The user wants to set up branch protection rules to enforce code quality standards and prevent accidental pushes to critical branches.
What to check first
- Run
git branch -ato confirm your main/master branch exists - Verify you have admin access to the repository (check Settings > Collaborators on GitHub)
- Confirm your default branch is set correctly (Settings > Branches > Default branch)
Steps
- Navigate to your repository on GitHub and click the Settings tab
- Click Branches in the left sidebar under "Code and automation"
- Click Add rule under "Branch protection rules"
- Enter the branch name pattern (e.g.,
main,master, orrelease/*for wildcard patterns) - Enable Require a pull request before merging and set the minimum number of required approvals (typically 1-2)
- Enable Require status checks to pass before merging and select your CI/CD checks (e.g.,
tests,lint,build) - Enable Require branches to be up to date before merging to prevent merge conflicts
- Enable Include administrators if you want these rules to apply to all users including admins
- Click Create to save the branch protection rule
Code
# Using PyGithub library to set branch protection programmatically
from github import Github
from github.GithubException import GithubException
# Authenticate with GitHub
g = Github("your_github_token")
repo = g.get_repo("owner/repo_name")
# Get the main branch
main_branch = repo.get_branch("main")
# Set up branch protection with required parameters
try:
repo.edit(
default_branch="main"
)
# Apply protection to main branch
main_branch.edit_protection(
strict=True, # Require branches to be up to date before merging
required_approving_review_count=2, # Require 2 approvals
dismiss_stale_reviews=True, # Dismiss outdated reviews when new commits are pushed
require_code_owner_reviews=True, # Require approval from code owners
required_status_checks=["tests", "lint", "build"], # Require these checks to pass
enforce_admins=True, # Apply rules to administrators too
allow_force_pushes=False, # Prevent force pushes
allow_deletions=False, # Prevent branch deletion
)
print("✓ Branch protection rules applied successfully to 'main'")
except GithubException as e:
print(f"✗ Error applying branch protection: {e.status} - {e.data['message']}")
Pitfalls
- Forgetting to enable required status checks: Without this, PRs can merge even if CI/CD pipelines fail — always specify your actual check names from your
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Workflow Automation Skills
Other Claude Code skills in the same category — free to download.
Git Workflow
Set up Git branching workflow (GitFlow, trunk-based)
Pre-Commit Hooks
Configure pre-commit hooks (Husky, lint-staged)
Auto Formatter
Set up auto-formatting (Prettier, ESLint)
Issue Template
Create GitHub issue and PR templates
Dependabot Setup
Configure Dependabot/Renovate for auto-updates
Release Workflow
Automate release workflow with changelogs
Code Owner Setup
Configure CODEOWNERS file
Auto Labeler
Auto-label PRs based on changed files
Want a Workflow Automation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.