gitlab-protected-branch
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseProtected Branch Skill
受保护分支技能
Branch protection management for GitLab using raw endpoint calls.
glab api使用原始端点调用管理GitLab的分支保护。
glab apiQuick Reference
快速参考
| Operation | Command Pattern | Risk |
|---|---|---|
| List protected | | - |
| Get protection | | - |
| Protect branch | | ⚠️ |
| Update protection | | ⚠️ |
| Unprotect branch | | ⚠️⚠️ |
Risk Legend: - Safe | ⚠️ Caution | ⚠️⚠️ Warning | ⚠️⚠️⚠️ Danger
| 操作 | 命令模板 | 风险 |
|---|---|---|
| 列出受保护分支 | | - |
| 获取保护规则 | | - |
| 保护分支 | | ⚠️ |
| 更新保护规则 | | ⚠️ |
| 取消分支保护 | | ⚠️⚠️ |
风险图例: - 安全 | ⚠️ 注意 | ⚠️⚠️ 警告 | ⚠️⚠️⚠️ 危险
When to Use This Skill
何时使用该技能
ALWAYS use when:
- User mentions "protect branch", "branch protection", "protected branches"
- User wants to restrict who can push/merge to a branch
- User mentions "force push", "code owners", "merge access"
- User wants to configure main/release branch security
NEVER use when:
- User wants to create/delete branches (use git or gitlab-repo)
- User wants to manage merge request approvals (different API)
- User wants to configure CI/CD for branches (use gitlab-ci)
请务必在以下场景使用:
- 用户提及"protect branch"、"branch protection"、"protected branches"
- 用户希望限制谁可以向分支推送/合并代码
- 用户提及"force push"、"code owners"、"merge access"
- 用户希望配置主分支/发布分支的安全性
请勿在以下场景使用:
- 用户想要创建/删除分支(使用git或gitlab-repo)
- 用户想要管理合并请求审批(使用不同的API)
- 用户想要为分支配置CI/CD(使用gitlab-ci)
API Prerequisites
API前置要求
Required Token Scopes:
apiPermissions:
- View protected branches: Developer+
- Manage protected branches: Maintainer+
Premium Features:
- Code owner approval: GitLab Premium
- Multiple access levels: GitLab Premium
所需令牌权限范围:
api权限:
- 查看受保护分支:开发者及以上角色
- 管理受保护分支:维护者及以上角色
高级功能:
- 代码所有者审批:GitLab Premium
- 多权限等级:GitLab Premium
Access Levels
权限等级
| 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 |
| 等级 | 数值 | 描述 |
|---|---|---|
| 无权限 | 0 | 无人可执行操作 |
| 开发者 | 30 | 开发者及以上角色 |
| 维护者 | 40 | 维护者及以上角色 |
| 管理员 | 60 | 仅实例管理员 |
Available Commands
可用命令
List Protected Branches
列出受保护分支
bash
undefinedbash
undefinedList all protected branches
List all protected branches
glab api projects/123/protected_branches --method GET
glab api projects/123/protected_branches --method GET
With pagination
With pagination
glab api projects/123/protected_branches --paginate
glab api projects/123/protected_branches --paginate
Using project path
Using project path
glab api "projects/$(echo 'mygroup/myproject' | jq -Rr @uri)/protected_branches"
undefinedglab api "projects/$(echo 'mygroup/myproject' | jq -Rr @uri)/protected_branches"
undefinedGet Protection Details
获取保护规则详情
bash
undefinedbash
undefinedGet protection for specific branch
Get protection for specific branch
glab api projects/123/protected_branches/main --method GET
glab api projects/123/protected_branches/main --method GET
Branch with special characters (URL-encode)
Branch with special characters (URL-encode)
glab api "projects/123/protected_branches/$(echo 'release/1.0' | jq -Rr @uri)"
glab api "projects/123/protected_branches/$(echo 'release/1.0' | jq -Rr @uri)"
Branch with wildcard pattern
Branch with wildcard pattern
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)"
undefinedglab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)"
undefinedProtect a Branch
保护分支
bash
undefinedbash
undefinedBasic protection (maintainers push, developers merge)
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
-f name="main"
-f push_access_level=40
-f merge_access_level=30
glab api projects/123/protected_branches --method POST
-f name="main"
-f push_access_level=40
-f merge_access_level=30
-f name="main"
-f push_access_level=40
-f merge_access_level=30
Strict protection (only maintainers)
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
-f name="main"
-f push_access_level=40
-f merge_access_level=40
-f allow_force_push=false
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
-f name="main"
-f push_access_level=40
-f merge_access_level=40
-f allow_force_push=false
With code owner approval (Premium)
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
-f name="main"
-f push_access_level=40
-f merge_access_level=30
-f code_owner_approval_required=true
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
-f name="main"
-f push_access_level=40
-f merge_access_level=30
-f code_owner_approval_required=true
Protect wildcard pattern
Protect wildcard pattern
glab api projects/123/protected_branches --method POST
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
glab api projects/123/protected_branches --method POST
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
Allow developers to push, anyone to merge
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
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f allow_force_push=false
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
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f allow_force_push=false
No direct push (only through MR)
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
-f name="main"
-f push_access_level=0
-f merge_access_level=30
undefinedglab api projects/123/protected_branches --method POST
-f name="main"
-f push_access_level=0
-f merge_access_level=30
-f name="main"
-f push_access_level=0
-f merge_access_level=30
undefinedUpdate Protection
更新保护规则
bash
undefinedbash
undefinedChange merge access level
Change merge access level
glab api projects/123/protected_branches/main --method PATCH
-f merge_access_level=40
-f merge_access_level=40
glab api projects/123/protected_branches/main --method PATCH
-f merge_access_level=40
-f merge_access_level=40
Enable code owner approval (Premium)
Enable code owner approval (Premium)
glab api projects/123/protected_branches/main --method PATCH
-f code_owner_approval_required=true
-f code_owner_approval_required=true
glab api projects/123/protected_branches/main --method PATCH
-f code_owner_approval_required=true
-f code_owner_approval_required=true
Allow force push (not recommended for main)
Allow force push (not recommended for main)
glab api projects/123/protected_branches/feature%2F* --method PATCH
-f allow_force_push=true
-f allow_force_push=true
undefinedglab api projects/123/protected_branches/feature%2F* --method PATCH
-f allow_force_push=true
-f allow_force_push=true
undefinedUnprotect Branch
取消分支保护
Warning: This removes all protection from the branch!
bash
undefined警告: 此操作会移除该分支的所有保护规则!
bash
undefinedUnprotect branch
Unprotect branch
glab api projects/123/protected_branches/main --method DELETE
glab api projects/123/protected_branches/main --method DELETE
Unprotect wildcard pattern (URL-encode)
Unprotect wildcard pattern (URL-encode)
glab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)" --method DELETE
undefinedglab api "projects/123/protected_branches/$(echo 'feature/*' | jq -Rr @uri)" --method DELETE
undefinedProtection Options
保护选项
| 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) |
| 选项 | 类型 | 描述 |
|---|---|---|
| 字符串 | 分支名称或通配符模式 |
| 整数 | 允许推送的角色(0, 30, 40, 60) |
| 整数 | 允许合并MR的角色(0, 30, 40, 60) |
| 整数 | 允许取消保护的角色(40, 60) |
| 布尔值 | 是否允许强制推送至该分支 |
| 布尔值 | 是否要求代码所有者审批(高级版) |
Wildcard Patterns
通配符模式
| Pattern | Matches |
|---|---|
| All branches |
| |
| |
| |
| |
| 模式 | 匹配对象 |
|---|---|
| 所有分支 |
| |
| |
| |
| |
Common Workflows
常见工作流
Workflow 1: Standard Branch Protection
工作流1: 标准分支保护
bash
undefinedbash
undefinedProtect main branch
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
-f name="main"
-f push_access_level=40
-f merge_access_level=30
-f allow_force_push=false
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
-f name="main"
-f push_access_level=40
-f merge_access_level=30
-f allow_force_push=false
Protect develop branch
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
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f allow_force_push=false
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
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f allow_force_push=false
Protect release branches
Protect release branches
glab api projects/123/protected_branches --method POST
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
undefinedglab api projects/123/protected_branches --method POST
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
undefinedWorkflow 2: Audit Current Protections
工作流2: 审计当前保护规则
bash
undefinedbash
undefinedList all protections with details
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"'
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"'
undefinedglab 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"'
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"'
undefinedWorkflow 3: Lock Down Production Branch
工作流3: 锁定生产分支
bash
undefinedbash
undefinedStrict protection: only maintainers, no force push, require code owners
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
-f name="production"
-f push_access_level=40
-f merge_access_level=40
-f allow_force_push=false
-f code_owner_approval_required=true
undefinedglab 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
-f name="production"
-f push_access_level=40
-f merge_access_level=40
-f allow_force_push=false
-f code_owner_approval_required=true
undefinedWorkflow 4: Temporarily Allow Push to Protected Branch
工作流4: 临时允许向受保护分支推送
bash
undefinedbash
undefined1. Check current protection
1. Check current protection
glab api projects/123/protected_branches/main
glab api projects/123/protected_branches/main
2. Update to allow developer push
2. Update to allow developer push
glab api projects/123/protected_branches/main --method PATCH
-f push_access_level=30
-f push_access_level=30
glab api projects/123/protected_branches/main --method PATCH
-f push_access_level=30
-f push_access_level=30
3. Do the work...
3. Do the work...
4. Restore protection
4. Restore protection
glab api projects/123/protected_branches/main --method PATCH
-f push_access_level=40
-f push_access_level=40
undefinedglab api projects/123/protected_branches/main --method PATCH
-f push_access_level=40
-f push_access_level=40
undefinedWorkflow 5: Set Up GitFlow Protection
工作流5: 配置GitFlow分支保护
bash
project_id=123bash
project_id=123Main - production (strict)
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
-f name="main"
-f push_access_level=0
-f merge_access_level=40
-f allow_force_push=false
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
-f name="main"
-f push_access_level=0
-f merge_access_level=40
-f allow_force_push=false
Develop - integration
Develop - integration
glab api projects/$project_id/protected_branches --method POST
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
glab api projects/$project_id/protected_branches --method POST
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
-f name="develop"
-f push_access_level=30
-f merge_access_level=30
Feature branches - allow developers
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
-f name="feature/*"
-f push_access_level=30
-f merge_access_level=30
glab api projects/$project_id/protected_branches --method POST
-f name="feature/*"
-f push_access_level=30
-f merge_access_level=30
-f name="feature/*"
-f push_access_level=30
-f merge_access_level=30
Release branches - maintainers only
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
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
glab api projects/$project_id/protected_branches --method POST
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
-f name="release/*"
-f push_access_level=40
-f merge_access_level=40
Hotfix branches - maintainers only
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
-f name="hotfix/*"
-f push_access_level=40
-f merge_access_level=40
undefinedglab api projects/$project_id/protected_branches --method POST
-f name="hotfix/*"
-f push_access_level=40
-f merge_access_level=40
-f name="hotfix/*"
-f push_access_level=40
-f merge_access_level=40
undefinedTroubleshooting
故障排查
| 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 |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 403 Forbidden | 非维护者角色 | 需要维护者及以上角色 |
| 404 Not Found | 分支不存在或未被保护 | 检查分支名称 |
| 400 Bad Request | 无效的权限等级 | 使用0、30、40或60 |
| 分支仍处于保护状态 | 通配符模式匹配 | 检查是否存在通配符保护规则 |
| 无法向受保护分支推送 | 权限等级过低 | 更新保护规则或获取更高角色权限 |
Best Practices
最佳实践
- Always protect main: At minimum, protect your default branch
- Use wildcards wisely: Protect instead of individual releases
release/* - Avoid force push on main: Set
allow_force_push=false - Document protections: Keep track of your branch protection strategy
- Review regularly: Audit protections periodically
- 始终保护主分支: 至少保护你的默认分支
- 合理使用通配符: 保护而非单个发布分支
release/* - 主分支禁止强制推送: 设置
allow_force_push=false - 记录保护规则: 记录你的分支保护策略
- 定期审核: 定期审计保护规则
Related Documentation
相关文档
- API Helpers
- Safeguards
- Quick Reference
- GitLab Protected Branches API
- API Helpers
- Safeguards
- Quick Reference
- GitLab Protected Branches API