How to Add CODEOWNERS to Your Git Repository: Step-by-Step Guide

Adding a CODEOWNERS file to your Git repository ensures that designated reviewers are automatically assigned to pull requests, helping to streamline the review process and maintain code quality.

Whether you're working on a team or managing contributions, setting up CODEOWNERS ensures that the right people review the changes.

In this step-by-step guide, let's check out how to add CODEOWNERS to your repository, complete with a demo.

How to Add CODEOWNERS to Your Git Repository

Steps to follow:

1. In your IDE, create a 'CODEOWNERS' file inside the '.github >> docs' folder on the master/main (default) branch.

2. Add the GitHub usernames of the repository collaborators who should act as reviewers for all pull requests (PRs) into the repository, then save the file.

Note: These users must be collaborators in the repository to be added as Code Owners.

3. After saving the changes, push the update to the master/main branch by following these steps:

  • Stage the changes- 'git add .'
  • Commit the changes- 'git commit -m "commit message"'.
  • Push the changes to the master or main branch- 'git push origin master' (or 'git push origin main' if using the main branch).

4. Confirm that your Code Owners changes are now reflected in the default branch (master/main).

5. To set Code Owners in branch rules, you need admin access to the repository. Navigate to the 'Settings' option.

6. Select the 'Branches' option from the navigation menu.

7. Enter the name of the branch for which you want to set branch rules. In this case, it's 'master'.

8. Ensure the following three options are checked in the branch rules list to enforce active Code Owners review-

- ‘Require a pull request before merging’.

- ‘Require approvals’.

- ‘Require review from Code Owners’.

9. Navigate to 'Pull requests'.

10. Create a pull request (PR) toward the master branch. After that, fill in the PR title and description.

11. Create the pull request (PR) after filling in the necessary PR information.

12. Once the PR is created, you'll notice that merging is blocked because it requires approval from at least one Code Owner.

That's it!

This is how you add CODEOWNERS to a Git repository.

Conclusion

Now that you've added a CODEOWNERS file to your Git repository, you can ensure that the right collaborators review all pull requests before they’re merged.

This keeps your project organized and helps maintain high-quality code reviews.

Still have questions?

Send an email to archana@leadwalnut.com, OR

Book a FREE consultation with an expert developer here.