Skip to content

Latest commit

 

History

History
131 lines (89 loc) · 3.35 KB

File metadata and controls

131 lines (89 loc) · 3.35 KB

Lab 6: Protect the main Branch

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.

Scenario

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.

Task 1: Create a CODEOWNERS File

  1. Ensure you're working on the main branch

    git checkout main
  2. At the root of the repository, create a new file named CODEOWNERS with the following contents

    Replace your-username with your GitHub usernam. Make sure to leave the @ symbol!

    # Game Project Code Owners
    
    * @your-username
    
  3. Add the CODEOWNERS file to the repository

    git add CODEOWNERS
    git commit -m "Add CODEOWNERS file"
  4. Push the changes to GitHub

    git push

Task 2: Protect the main Branch

  1. In your browser, navigate to your repository on GitHub

  2. Click on the Settings tab

  3. Expand Rules, then click Rulesets

  4. Click New ruleset, then New branch ruleset

  5. In the Ruleset name text field, enter a name for your rulset (e.g. Default Branch)

  6. Set the Enforcement status to Active

    New Ruleset

  7. In the Target branches section, click Add target, then click Include default branch!

    Include Default Branch

  8. In the Rules section, set the following:

    • Require a pull request before merging: Enabled
    • Required approvals: 1
    • Require review from Code Owners: Enabled

    Rules

  9. Click Create

Task 3: Try to Push Directly to main

Now that the main branch is protected, let's see what happens when we try to push changes directly to main.

  1. In your terminal, make sure you're on the main branch

    git checkout main
  2. Make a change any file in the repository

    For example, you could add a new line to the README.md file

    # GitHub Intermediate - Project Repository
    
    This is an update to the README.md file!
  3. Add and commit the changes

    git add README.md
    git commit -m "Update README.md"
  4. 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 main branch.

    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'
    

Task 4: Reset the main Branch

  1. In your terminal, reset the main branch to the previous commit

    git reset --hard HEAD~1

Need Help?

If you're having trouble with any of the steps, you can ask for help in the meeting chat.