Skip to main content
Welcome back! In this lesson, we’ll walk through enabling branch protection on GitHub and establish a workflow using feature branches and pull requests.

Enabling Branch Protection

Enabling branch protection helps maintain code quality and prevents direct pushes to critical branches.
  1. In your repository, navigate to Settings > Branches.
  2. Click Add branch protection rule.
  3. Set Branch name pattern to main.
  4. Enable Require a pull request before merging.
  5. Uncheck Require approvals if you’re working solo.
  6. Scroll down and click Create.
The image shows a GitHub settings page for creating a new branch protection rule, with options to require pull requests and approvals before merging into the main branch.
Once created, your protected rule appears in the list:
The image shows a GitHub repository settings page, specifically the "Branches" section, where the default branch is set to "main" and branch protection rules are being managed.

Working with Feature Branches

Avoid committing directly to main. Always use a dedicated feature branch for your changes.
First, verify your current branch in VS Code:

Creating a Feature Branch

Switch to a new branch for your task:

Updating the README

  1. Open README.md.
  2. Change the heading from #### to # for an H1 title.
  3. Preview your changes to confirm the update.

Staging and Committing

Stage and commit the revision:

Pushing the Feature Branch

Push your branch to GitHub:
You should see a message prompting you to create a pull request:

Creating and Merging a Pull Request

  1. Click Compare & pull request in the GitHub banner.
  2. On the Open a pull request page:
    • Title: Update README.md to use H1 heading
    • Description: Update the README file to make the first line a heading
  3. Review your changes and click Create pull request.
The image shows a GitHub interface where a user is preparing to create a pull request to update a README.md file. The changes include making the first line a heading, with one addition and one deletion highlighted.
  1. Click Merge pull request, then Confirm merge (approvals are not required for solo projects).
The image shows a GitHub pull request page for a project named "gcp-devops-project," where a pull request titled "Update readme.md file #1" has been successfully merged and closed.
After merging, your main branch includes the change. This clear, repeatable process scales with team size and project complexity. That’s it for this lesson. See you in the next one!

Watch Video