Bird
Raised Fist0
Gitdevops~10 mins

Branch protection rules in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Branch protection rules
Start: Create branch
↓
Set branch protection rules
↓
Push or PR to branch
↓
Check rules: Is push/merge allowed?
No→Block action, show error
Yes↓
Allow push/merge
↓
Branch updated
This flow shows how branch protection rules check every push or merge to a branch and block it if rules are not met.
Execution Sample
Git
git branch main
# Set protection: require PR review
# Try to push directly to main
# Push blocked by protection
# Create PR and get review
# Merge PR to main
This example shows setting a branch protection rule on 'main' and how direct pushes are blocked, requiring a pull request with review.
Process Table
StepActionBranchProtection Rule CheckResultMessage
1Create branchmainNo check neededBranch createdBranch 'main' created locally
2Set protection rulemainRequire PR review before mergeRule setBranch protection rule applied to 'main'
3Attempt direct pushmainCheck direct push allowed?BlockedPush rejected: direct push not allowed, PR required
4Create pull requestmainPR createdPending reviewPull request opened for 'main'
5Review PRmainCheck review approvalApprovedPR approved by reviewer
6Merge PRmainCheck merge allowedMergedPR merged into 'main' branch
💡 Direct push blocked at step 3 due to branch protection requiring PR review
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
branch_protectionnonerequire PR reviewrequire PR reviewrequire PR reviewrequire PR review
branch_statecreatedcreatedpush blockedPR approvedmerged
push_allowedtruefalsefalsefalsefalse
pr_statusnonenonenoneapprovedmerged
Key Moments - 3 Insights
Why is the direct push blocked even though the branch exists?
Because the branch protection rule set at step 2 requires pull request review before merging, so direct pushes are not allowed as shown in step 3.
What happens if the pull request is not approved?
The merge will be blocked at step 6 because the protection rule requires approval, so the branch state remains unchanged until approval.
Can you push directly after the PR is merged?
No, direct pushes remain blocked by the branch protection rule. All changes must go through pull requests with reviews.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of the direct push attempt at step 3?
ABlocked with error message
BAllowed and branch updated
CIgnored silently
DPush queued for later
💡 Hint
Check the 'Result' and 'Message' columns at step 3 in the execution table
At which step does the branch protection rule get applied?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the 'Action' and 'Protection Rule Check' columns in the execution table
If the PR was not approved, what would be the branch state after step 5?
Amerged
BPR open but not approved
Cpush blocked
DPR approved
💡 Hint
Refer to the 'pr_status' variable in the variable tracker after step 5
Concept Snapshot
Branch protection rules prevent direct pushes or merges that don't meet set conditions.
Common rules include requiring PR reviews, status checks, or restricting who can push.
When a rule blocks an action, Git rejects the push or merge with a clear message.
To update protected branches, follow the rules: create PRs, get reviews, then merge.
This keeps important branches safe and stable.
Full Transcript
Branch protection rules are settings on Git branches that control how changes can be made. The flow starts with creating a branch, then setting protection rules like requiring pull request reviews. When someone tries to push directly to the protected branch, Git checks the rules and blocks the push if it doesn't meet them. Instead, the user creates a pull request, which must be reviewed and approved. After approval, the pull request can be merged, updating the branch. Variables like branch protection status, push permission, and pull request state change step-by-step during this process. Key points include understanding why direct pushes are blocked and how approvals enable merges. This ensures the branch stays stable and changes are reviewed.

Practice

(1/5)
1. What is the main purpose of branch protection rules in Git repositories?
easy
A. To prevent direct changes to important branches without review or checks
B. To delete branches automatically after merging
C. To speed up the cloning process of the repository
D. To allow anyone to push changes without restrictions

Solution

  1. Step 1: Understand branch protection rules

    Branch protection rules are designed to keep important branches safe by enforcing rules like requiring reviews or passing tests before changes can be merged.
  2. Step 2: Identify the correct purpose

    Options A, B, and D describe unrelated or incorrect behaviors. Only To prevent direct changes to important branches without review or checks correctly describes the purpose of branch protection rules.
  3. Final Answer:

    To prevent direct changes to important branches without review or checks -> Option A
  4. Quick Check:

    Branch protection = prevent unsafe direct changes [OK]
Hint: Branch protection means safe branches, no direct unsafe changes [OK]
Common Mistakes:
  • Thinking branch protection deletes branches
  • Confusing branch protection with performance features
  • Assuming branch protection allows unrestricted pushes
2. Which of the following is the correct way to enable branch protection for the main branch on GitHub via the web interface?
easy
A. Create a file named branch-protect.yml in the root directory
B. Run git protect main in the terminal
C. Go to Settings > Branches > Add rule > Specify 'main' > Select protections > Save
D. Push a branch named protected/main to enable protection

Solution

  1. Step 1: Know how to set branch protection on GitHub

    Branch protection rules are set in the repository's Settings under Branches by adding a rule for the branch name and selecting protections.
  2. Step 2: Evaluate options

    Go to Settings > Branches > Add rule > Specify 'main' > Select protections > Save describes the correct web interface steps. Options B, C, and D are invalid commands or methods not supported by GitHub.
  3. Final Answer:

    Go to Settings > Branches > Add rule > Specify 'main' > Select protections > Save -> Option C
  4. Quick Check:

    GitHub branch protection set via Settings > Branches [OK]
Hint: Branch protection is set in repo Settings under Branches [OK]
Common Mistakes:
  • Trying to use git CLI commands that don't exist
  • Assuming config files control branch protection
  • Thinking branch protection is automatic on branch creation
3. Given this branch protection rule requiring pull request reviews before merging, what happens if a user tries to push directly to the protected branch?
Rule: Require pull request reviews before merging
medium
A. The push is rejected with an error message
B. The push succeeds without any checks
C. The branch is automatically deleted
D. The push creates a new branch instead

Solution

  1. Step 1: Understand the rule effect

    Requiring pull request reviews means direct pushes to the branch are blocked to enforce code review before changes.
  2. Step 2: Predict the push behavior

    Because direct pushes are blocked, the push attempt will be rejected with an error message. Options A, B, and C are incorrect behaviors.
  3. Final Answer:

    The push is rejected with an error message -> Option A
  4. Quick Check:

    Require review blocks direct push = push rejected [OK]
Hint: Require review means no direct push allowed [OK]
Common Mistakes:
  • Assuming push silently creates new branch
  • Thinking push succeeds without review
  • Believing branch gets deleted on push
4. A developer sets a branch protection rule to require status checks before merging, but merges are still allowed without passing checks. What is the likely cause?
medium
A. The developer forgot to push the branch
B. The status checks are not selected/enabled in the branch protection settings
C. The repository has no branches
D. The branch protection rules only apply to forks

Solution

  1. Step 1: Analyze the protection rule setup

    Requiring status checks means you must select which checks must pass in the branch protection settings.
  2. Step 2: Identify why merges bypass checks

    If merges happen without passing checks, it usually means the required checks were not properly selected or enabled in the settings.
  3. Final Answer:

    The status checks are not selected/enabled in the branch protection settings -> Option B
  4. Quick Check:

    Missing selected checks = merges bypass checks [OK]
Hint: Enable and select status checks in protection settings [OK]
Common Mistakes:
  • Assuming branch protection applies only to forks
  • Thinking pushing branch affects protection
  • Ignoring the need to select specific status checks
5. You want to protect the release branch so that only team leads can push directly, and everyone else must use pull requests with reviews. Which branch protection settings should you apply?
hard
A. Require status checks but allow anyone to push directly
B. Enable 'Require signed commits' only
C. Disable all protections and trust the team
D. Enable 'Restrict who can push' to team leads and require pull request reviews for others

Solution

  1. Step 1: Understand the requirement

    The goal is to allow only team leads to push directly and force others to use pull requests with reviews.
  2. Step 2: Choose correct settings

    Enabling 'Restrict who can push' to team leads limits direct pushes to them. Requiring pull request reviews ensures others must use PRs. Options B, C, and D do not meet both conditions.
  3. Final Answer:

    Enable 'Restrict who can push' to team leads and require pull request reviews for others -> Option D
  4. Quick Check:

    Restrict push + require reviews = controlled direct push [OK]
Hint: Restrict push to leads + require reviews for others [OK]
Common Mistakes:
  • Only requiring signed commits without push restrictions
  • Disabling protections and trusting team
  • Allowing anyone to push directly with status checks only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard