Skip to Content

GitHub Foundations: How to Require Approvals for Pull Requests in Specific Repository Areas?

Learn how to use CODEOWNERS files and enable required reviews to ensure pull requests for certain areas of your GitHub repository are only merged after approval from specific users or teams. Improve your repository’s security and maintain code quality with this best practice.

Table of Contents

Question

How can you ensure that pull requests for a given area of the repository aren’t merged unless certain users or teams approve?

A. Clearly explain the pull request policy in CONTRIBUTING.md.
B. Use a CODEOWNERS file and enable required reviews.
C. Add a table mapping directory paths to required users in SECURITY.md.

Answer

B. Use a CODEOWNERS file and enable required reviews.

Explanation

A CODEOWNERS file enables you to assign users or teams as required reviewers using the same syntax as .gitingore files.

To ensure that pull requests for a given area of the repository aren’t merged unless certain users or teams approve, the best approach is to use a CODEOWNERS file and enable required reviews (Option B).

A CODEOWNERS file is a special file in your repository that defines which individuals or teams are responsible for code in specific directories or files. When a pull request is opened that modifies code in a directory or file listed in the CODEOWNERS file, GitHub automatically requests reviews from the specified owners.

To set up a CODEOWNERS file:

1. Create a new file named “CODEOWNERS” (without any extension) in the root, “.github”, or “docs” directory of your repository.

2. In the CODEOWNERS file, specify the directory paths or file patterns followed by the GitHub usernames or team names (prefixed with “@”) that should be required to review changes. For example:

/critical/path/ @user1 @user2
*.js @js-team

3. Commit the CODEOWNERS file to your repository.

Next, enable required reviews in your repository settings:

  1. Go to your repository’s “Settings” tab.
  2. Click on “Branches” in the left sidebar.
  3. Under “Branch protection rules”, click “Add rule”.
  4. Specify the branch name pattern (e.g., “main” or “develop”) and enable the “Require pull request reviews before merging” option.
  5. Configure the desired number of approvals required and any other settings.
  6. Save the branch protection rule.

With a CODEOWNERS file in place and required reviews enabled, pull requests that modify code in the specified areas will automatically request reviews from the designated users or teams. The pull request cannot be merged until the required approvals are obtained, ensuring that changes are reviewed and approved by the appropriate individuals before being incorporated into the protected branch.

While clearly explaining the pull request policy in CONTRIBUTING.md (Option A) is a good practice for communication, it doesn’t enforce the requirement programmatically. Adding a table mapping directory paths to required users in SECURITY.md (Option C) is not a standard convention and would not have the same effect as using a CODEOWNERS file.

Therefore, using a CODEOWNERS file in conjunction with enabling required reviews is the most effective way to ensure that pull requests for specific areas of the repository are only merged after receiving approvals from designated users or teams.

GitHub Foundations certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the GitHub Foundations exam and earn GitHub Foundations certification.