Skip to content

Say hello to the new PR page.

Read more

How to set up branch protection rules in GitHub

Kenny DuMez
Kenny DuMez
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Branch protection rules in GitHub are important for maintaining the integrity of critical branches, such as main or release. These rules help teams enforce code review policies, manage access, and automate workflows to ensure that changes meet quality standards before they are merged. This guide provides a step-by-step approach to setting up branch protection rules in GitHub.

Branch protection rules in GitHub are designed to prevent direct changes to important branches and ensure that all modifications go through a defined workflow. This feature helps teams:

  • Prevent errors and code conflicts on important branches.
  • Enforce code reviews before merging.
  • Automate testing and other checks that confirm changes are safe to deploy.

Here’s how to configure branch protection rules to secure your branches and streamline your development process:

  • Open your GitHub repository.
  • Click on the 'Settings' tab near the top of the repository page.
  • In the left sidebar under the repository settings, click on 'Branches'.
  • You will see a section labeled 'Branch protection rules'.
  • Click on 'Add rule' to create a new set of rules, or select an existing rule to edit.
  • Apply rule to: Enter the branch name pattern, such as main or release*, to which these rules will apply.
  • Branch name pattern: Use wildcard characters to apply rules to multiple branches if necessary.

Here are some common settings you might consider enabling:

  • Require pull request reviews before merging: Ensure that at least one other person reviews the code before it can be merged.
  • Require status checks to pass before merging: Set up required status checks such as continuous integration tests that must pass before merging.
  • Require signed commits: Require all commits to be signed with a GPG or S/MIME signature.
  • Include administrators: Apply these rules to everyone, including repository administrators.
  • Restrict who can push to matching branches: Specify which users or teams can push to the protected branch.
  • Review and approve changes: Ensure that only approved changes are merged into the protected branches.
  • Automate deployment pipelines: Utilize GitHub Actions or other CI/CD tools to automate builds, tests, and deployments based on successful merges to protected branches.
  • After configuring all the necessary settings, click 'Save changes' at the bottom of the page to enforce the branch protection rules.
  • Regularly review and update your branch protection settings: Keep your rules up-to-date with new development practices or team structures.
  • Educate your team on the importance of branch protection: Make sure all team members understand why these rules are in place and how to work with them.
  • Use branch protection as part of a comprehensive security and quality strategy: Combine branch protections with other practices such as code scanning and security reviews to maintain high standards.
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone