In this lab, we're going to add a CODEOWNERS file, enable branch protections,
and see what happens when we try to push any changes directly to main.
Up until this point, we've been pushing changes directly to the default branch,
main. When working with multiple developers in the same repo(s), this can
cause many conflicts and other problems.
-
Ensure you're working on the
mainbranchgit checkout main
-
At the root of the repository, create a new file named
CODEOWNERSwith the following contentsReplace
your-usernamewith your GitHub usernam. Make sure to leave the@symbol!# Game Project Code Owners * @your-username -
Add the
CODEOWNERSfile to the repositorygit add CODEOWNERS git commit -m "Add CODEOWNERS file" -
Push the changes to GitHub
git push
-
In your browser, navigate to your repository on GitHub
-
Click on the Settings tab
-
Expand Rules, then click Rulesets
-
Click New ruleset, then New branch ruleset
-
In the Ruleset name text field, enter a name for your rulset (e.g.
Default Branch) -
Set the Enforcement status to Active
-
In the Target branches section, click Add target, then click Include default branch!
-
In the Rules section, set the following:
- Require a pull request before merging: Enabled
- Required approvals:
1 - Require review from Code Owners: Enabled
-
Click Create
Now that the main branch is protected, let's see what happens when we try to
push changes directly to main.
-
In your terminal, make sure you're on the
mainbranchgit checkout main
-
Make a change any file in the repository
For example, you could add a new line to the
README.mdfile# GitHub Intermediate - Project Repository This is an update to the README.md file! -
Add and commit the changes
git add README.md git commit -m "Update README.md" -
Push the changes to GitHub
git push
At this point, you should see an error message in your terminal, indicating that you cannot push changes directly to the
mainbranch.remote: error: GH013: Repository rule violations found for refs/heads/main. remote: Review all repository rules at https://github.com/githubschool/gh-github-intermediate-template/rules?ref=refs%2Fheads%2Fmain remote: remote: - Changes must be made through a pull request. remote: To github.com:githubschool/gh-github-intermediate-template.git ! [remote rejected] main -> main (push declined due to repository rule violations) error: failed to push some refs to 'github.com:githubschool/gh-github-intermediate-template.git'
-
In your terminal, reset the
mainbranch to the previous commitgit reset --hard HEAD~1
If you're having trouble with any of the steps, you can ask for help in the meeting chat.


