Enforcing pull request requirements and restrictions in GitHub

Sara Verdi
Sara Verdi
Graphite software engineer


Note

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


Enforcing pull request requirements and restrictions in GitHub is not just a best practice—it's a cornerstone for maintaining high code quality and encouraging collaboration. By ensuring that every change undergoes rigorous review before being merged into the main branch, teams can prevent potential regressions and enhance overall project integrity.

This guide will walk you through the process of setting up and enforcing pull request requirements, leveraging GitHub’s built-in features alongside Graphite Protections. We’ll also walk through the steps to require a pull request before merging, highlight the significance of thorough pull request reviews, and provide solutions for overcoming common hurdles.

Pull request requirements are rules set at the repository level to ensure that all contributions are reviewed and approved before merging. These can include:

  • Requiring pull request reviews from designated team members.
  • Ensuring that the pull request is associated with an issue.
  • Setting up status checks that must pass before merging.

By enforcing these requirements, teams can ensure a higher quality of code and foster better collaboration among developers.

To set up pull request requirements (admin access required):

  1. Navigate to your GitHub repository: Go to the main page of your repository on GitHub.

  2. Go to the settings tab: Click on the "Settings" tab located in the top menu.

  3. Select branches: In the left sidebar, click on "Branches."

  4. Add a branch protection rule: Under "Branch protection rules," click on "Add rule."

  5. Specify the branch name pattern: Enter the name of the branch you want to protect (e.g., main). You can use wildcard patterns like * for broader rules.

  6. Require pull request reviews before merging: Check the box for "Require pull request reviews before merging." You can also specify the number of required approvals.

  7. Require status checks to pass before merging: Check "Require status checks to pass before merging." This ensures that automated tests must pass before the pull request can be merged.

  8. Save changes: Click "Create" or "Save changes" at the bottom of the page to apply your settings.

To enforce a policy that requires a pull request before merging changes into the main branch, follow these steps:

  1. Ensure the branch protection rule is set up as described above.
  2. Inform your team: Make sure all team members are aware that all changes must be made via pull requests.
  3. Enforce additional constraints using Graphite Protections: Graphite Protections can add additional rules and automation to your workflow. For instance, you can require reviews from specific stakeholders on the team, define required CI at the directory level in a monorepo, and easily override merge requirements when specific engineers are out of office.

Sometimes, you may encounter issues with enforcing pull request requirements.

If it's not working as expected, check the following:

  • Branch protection rules: Ensure that the branch protection rule is correctly configured and applied to the intended branch.
  • Team permissions: Verify that team members have the necessary permissions to create pull requests.
  • Conflicting settings: Check for any conflicting settings in the repository or organization settings that may override your branch protection rules.

Enforcing pull request reviews is an important aspect of maintaining code quality. Here's how to ensure that reviews are required:

  1. Set the number of required reviews: In the branch protection settings, specify how many reviews are necessary before a pull request can be merged. For example, if you set it to 2, at least two team members must approve the pull request.

  2. Use required reviewers: You can specify certain team members or teams who must review the pull request. This feature ensures that critical changes receive scrutiny from knowledgeable team members.

  3. Integrate with Graphite Protections: You can create Graphite Protections to automatically enforce required reviewers. If the required reviews are not present, Graphite can prevent the pull request from merging.

  • Communicate clearly: Ensure that all team members understand the importance of pull request requirements and how to adhere to them.
  • Use templates: Create pull request templates to guide developers on what information to provide when submitting a pull request.
  • Monitor compliance: Regularly review the effectiveness of your pull request policies and adjust them as necessary to improve the workflow.
  • Leverage tools: Utilize tools like Graphite Protections to automate and enforce your policies.

Enforcing pull request requirements and restrictions in GitHub ensures a high standard of code quality and encourages collaboration. By requiring pull requests before merging, leveraging review processes, and utilizing tools like Graphite Protections, teams can foster a more efficient and reliable development environment. Implement these practices to enhance your GitHub workflow and maintain high standards in your codebase.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2