0
0
Gitdevops~30 mins

Branch protection rules in Git - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting Up Branch Protection Rules in Git
📖 Scenario: You are working on a team project using Git. To keep the main branch safe from accidental changes, you want to set up branch protection rules. These rules will help prevent direct pushes and require pull requests for changes.
🎯 Goal: Learn how to create a branch protection rule for the main branch using GitHub CLI commands to enforce safe collaboration practices.
📋 What You'll Learn
Create a branch protection rule for the main branch
Require pull request reviews before merging
Prevent direct pushes to the main branch
Enable status checks to pass before merging
💡 Why This Matters
🌍 Real World
Branch protection rules help teams avoid mistakes by enforcing code review and testing before changes reach important branches like main or master.
💼 Career
Understanding how to set branch protection rules is essential for DevOps engineers and developers working in teams to maintain code quality and secure collaboration.
Progress0 / 4 steps
1
Create a GitHub repository called demo-repo
Use the GitHub CLI command gh repo create demo-repo --public --confirm to create a new public repository named demo-repo.
Git
Need a hint?

Use gh repo create with the repository name demo-repo, make it public, and confirm automatically.

2
Set a branch protection rule variable for the main branch
Create a variable called branch_name and set it to the string main to specify the branch you want to protect.
Git
Need a hint?

Use branch_name="main" to store the branch name.

3
Apply branch protection rules to require pull request reviews and prevent direct pushes
Use the GitHub CLI command gh api to create a branch protection rule on the repository demo-repo for the branch stored in branch_name. Require pull request reviews, prevent direct pushes, and require status checks to pass before merging.
Git
Need a hint?

Use gh api --method PUT with the correct endpoint and flags to set protection rules.

4
Verify the branch protection rules are applied
Use the GitHub CLI command gh api to get the branch protection settings for the main branch in demo-repo and print the output.
Git
Need a hint?

Use gh api /repos/:owner/demo-repo/branches/$branch_name/protection to fetch and print the protection settings.