0
0
Gitdevops~10 mins

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

Choose your learning style9 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?
NoBlock 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.