Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

GitLab Code – Protected Git Branches – Tutorial Guide

Here is a comprehensive tutorial for understanding and configuring Protected Branches in GitLab 18.0, based on the image you’ve provided and the latest official documentation.


šŸ” GitLab Protected Branches – Tutorial Guide (v18.0)

šŸ“˜ Purpose:

Protecting branches in GitLab helps teams:

  • Prevent force pushes
  • Restrict who can merge or push
  • Enforce code review via merge requests
  • Safeguard main/stable branches from accidental or unauthorized changes

šŸ”‘ What is a Protected Branch?

A protected branch is a Git branch that has rules and restrictions applied to prevent:

  • Direct pushes (without approvals or pipelines)
  • Deletion by mistake
  • Bypassing CI/CD requirements

GitLab typically uses it for main, master, release/*, or production.


āœ… Capabilities of Protected Branches

RestrictionDescription
Prevent Direct PushesOnly selected roles can push
Prevent Force PushesForce-push is disabled
Restrict Who Can MergeOnly selected users/roles can merge
Require Merge RequestsEnforce review before integration
Status Check Enforcement (EE+)Only allow merge if pipelines pass

šŸ› ļø How to Protect a Branch in GitLab (GitLab Cloud / v18.0+)

šŸ”¹ Option 1: Using Branch Rules (Recommended)

Step-by-Step:

  1. Go to your Project → Settings → Repository
  2. Scroll to Branch Rules
  3. Click ā€œAdd branch ruleā€
  4. In the modal:
    • Branch name: e.g., main or release/*
    • Allowed to push: Choose Maintainers, Developers + Maintainers, or custom users/groups
    • Allowed to merge: Choose same
    • Optionally enable:
      • āœ… Require merge request approvals
      • āœ… Require successful pipeline
  5. Click ā€œCreateā€

šŸŽÆ Now the branch is protected under branch rules — future pushes & merges are restricted.


šŸ”¹ Option 2: [Legacy] Using Protected Branches Panel (Deprecated)

In GitLab 18.0+, this is deprecated and migrated under Branch Rules UI.
Still visible under:
Settings → Repository → Protected branches section.

Here:

  • You can protect/unprotect branches
  • Define who can push/merge
  • Redirects to Branch Rules now

šŸŽÆ Best Practices

  • āœ… Protect all main, release, and production branches
  • āœ… Require merge requests to enforce peer reviews
  • āœ… Require pipeline success before merge (CI/CD)
  • āœ… Allow only maintainers to merge into protected branches
  • 🚫 Avoid giving push access to Developers on protected branches unless justified

šŸ’” Real-World Example: Protect main Branch

SettingValue
Branch namemain
Allowed to pushMaintainers only
Allowed to mergeMaintainers only
Require pipelineāœ… Enabled
Require approvalsāœ… Enabled (2 reviewers)

šŸ“Ž CLI Tip (Git)

Once protected:

# Direct push will fail (if you're not allowed)
git push origin main
# You must open a Merge Request instead
Code language: PHP (php)

šŸ“· Reference Screenshot Explanation (from your image)

  • āœ… Branch Rules are shown for main (default, protected)
  • āœ… Add new rules via “Add branch rule”
  • šŸ”’ Protected Branches section redirects to branch rules (GitLab is deprecating old interface)

🧪 Need More?


Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x