GCP DevOps Project

Sprint 01

Demo Setup Github repo according to DevOps best practice 02

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

Note

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.
Branch Protection OptionDescriptionStatus
Require pull request before mergingEnforce all changes to go through a pull requestEnabled
Require approvalsMandate review approvals before mergingDisabled

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

Warning

Avoid committing directly to main. Always use a dedicated feature branch for your changes.

First, verify your current branch in VS Code:

git branch
* main

Creating a Feature Branch

Switch to a new branch for your task:

git checkout -b feature/task-02
git branch
* feature/task-02
  main

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:

git add README.md
git status
# On branch feature/task-02
# Changes to be committed:
git commit -m "Update README.md to use H1 heading"

Pushing the Feature Branch

Push your branch to GitHub:

git push origin feature/task-02

You should see a message prompting you to create a pull request:

remote: Create a pull request for 'feature/task-02' on GitHub by visiting:
remote:   https://github.com/learnwithraghu/gcp-devops-project/pull/new/feature/task-02

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

Watch video content

Previous
Task 3Setup Github repo according to DevOps best practice