Loading...
Loading...
GitLab protected branch operations via API. ALWAYS use this skill when user wants to: (1) view branch protection rules, (2) protect/unprotect branches, (3) configure push/merge access levels, (4) set up code owner approval requirements.
npx skill4agent add grandcamel/gitlab-assistant-skills gitlab-protected-branchglab api| Operation | Command Pattern | Risk |
|---|---|---|
| List protected | | - |
| Get protection | | - |
| Protect branch | | ⚠️ |
| Update protection | | ⚠️ |
| Unprotect branch | | ⚠️⚠️ |
api| Level | Value | Description |
|---|---|---|
| No access | 0 | Nobody can perform action |
| Developer | 30 | Developers and above |
| Maintainer | 40 | Maintainers and above |
| Admin | 60 | Instance admins only |
# List all protected branches
glab api projects/123/protected_branches --method GET
# With pagination
glab api projects/123/protected_branches --paginate
# Using project path
glab api "projects/$(echo 'mygroup/myproject' | jq -Rr @uri)/protected_branches"# Get protection for specific branch
glab api projects/123/protected_branches/main --method GET
# Branch with special characters (URL-encode)
glab api "projects/123/protected_branches/$(echo 'release/1.0' | jq -Rr @uri)"
# Branch with wildcard pattern
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)"# Basic protection (maintainers push, developers merge)
glab api projects/123/protected_branches --method POST \
-f name="main" \
-f push_access_level=40 \
-f merge_access_level=30
# Strict protection (only maintainers)
glab api projects/123/protected_branches --method POST \
-f name="main" \
-f push_access_level=40 \
-f merge_access_level=40 \
-f allow_force_push=false
# With code owner approval (Premium)
glab api projects/123/protected_branches --method POST \
-f name="main" \
-f push_access_level=40 \
-f merge_access_level=30 \
-f code_owner_approval_required=true
# Protect wildcard pattern
glab api projects/123/protected_branches --method POST \
-f name="release/*" \
-f push_access_level=40 \
-f merge_access_level=40
# Allow developers to push, anyone to merge
glab api projects/123/protected_branches --method POST \
-f name="develop" \
-f push_access_level=30 \
-f merge_access_level=30 \
-f allow_force_push=false
# No direct push (only through MR)
glab api projects/123/protected_branches --method POST \
-f name="main" \
-f push_access_level=0 \
-f merge_access_level=30# Change merge access level
glab api projects/123/protected_branches/main --method PATCH \
-f merge_access_level=40
# Enable code owner approval (Premium)
glab api projects/123/protected_branches/main --method PATCH \
-f code_owner_approval_required=true
# Allow force push (not recommended for main)
glab api projects/123/protected_branches/feature%2F* --method PATCH \
-f allow_force_push=true# Unprotect branch
glab api projects/123/protected_branches/main --method DELETE
# Unprotect wildcard pattern (URL-encode)
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)" --method DELETE| Option | Type | Description |
|---|---|---|
| string | Branch name or wildcard pattern |
| integer | Who can push (0, 30, 40, 60) |
| integer | Who can merge MRs (0, 30, 40, 60) |
| integer | Who can unprotect (40, 60) |
| boolean | Allow force push to branch |
| boolean | Require code owner approval (Premium) |
| Pattern | Matches |
|---|---|
| All branches |
| |
| |
| |
| |
# Protect main branch
glab api projects/123/protected_branches --method POST \
-f name="main" \
-f push_access_level=40 \
-f merge_access_level=30 \
-f allow_force_push=false
# Protect develop branch
glab api projects/123/protected_branches --method POST \
-f name="develop" \
-f push_access_level=30 \
-f merge_access_level=30 \
-f allow_force_push=false
# Protect release branches
glab api projects/123/protected_branches --method POST \
-f name="release/*" \
-f push_access_level=40 \
-f merge_access_level=40# List all protections with details
glab api projects/123/protected_branches --paginate | \
jq -r '.[] | "Branch: \(.name)\n Push: \(.push_access_levels[0].access_level_description // "none")\n Merge: \(.merge_access_levels[0].access_level_description // "none")\n Force Push: \(.allow_force_push)\n"'# Strict protection: only maintainers, no force push, require code owners
glab api projects/123/protected_branches --method POST \
-f name="production" \
-f push_access_level=40 \
-f merge_access_level=40 \
-f allow_force_push=false \
-f code_owner_approval_required=true# 1. Check current protection
glab api projects/123/protected_branches/main
# 2. Update to allow developer push
glab api projects/123/protected_branches/main --method PATCH \
-f push_access_level=30
# 3. Do the work...
# 4. Restore protection
glab api projects/123/protected_branches/main --method PATCH \
-f push_access_level=40project_id=123
# Main - production (strict)
glab api projects/$project_id/protected_branches --method POST \
-f name="main" \
-f push_access_level=0 \
-f merge_access_level=40 \
-f allow_force_push=false
# Develop - integration
glab api projects/$project_id/protected_branches --method POST \
-f name="develop" \
-f push_access_level=30 \
-f merge_access_level=30
# Feature branches - allow developers
glab api projects/$project_id/protected_branches --method POST \
-f name="feature/*" \
-f push_access_level=30 \
-f merge_access_level=30
# Release branches - maintainers only
glab api projects/$project_id/protected_branches --method POST \
-f name="release/*" \
-f push_access_level=40 \
-f merge_access_level=40
# Hotfix branches - maintainers only
glab api projects/$project_id/protected_branches --method POST \
-f name="hotfix/*" \
-f push_access_level=40 \
-f merge_access_level=40| Issue | Cause | Solution |
|---|---|---|
| 403 Forbidden | Not maintainer | Need Maintainer+ role |
| 404 Not Found | Branch doesn't exist or not protected | Check branch name |
| 400 Bad Request | Invalid access level | Use 0, 30, 40, or 60 |
| Branch still protected | Pattern match | Check for wildcard patterns |
| Cannot push to protected | Access level too low | Update protection or get higher role |
release/*allow_force_push=false