glab
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitLab CLI (glab) Skill
GitLab CLI(glab)技能指南
Provides guidance for using , the official GitLab CLI, to perform GitLab operations from the terminal.
glab本指南提供如何使用官方GitLab CLI工具从终端执行GitLab操作的相关指导。
glabWhen to Use This Skill
何时使用本技能
Invoke when the user needs to:
- Create, review, or manage merge requests
- Work with GitLab issues
- Monitor or trigger CI/CD pipelines
- Clone or manage repositories
- Perform any GitLab operation from the command line
当用户需要以下操作时,可调用本技能:
- 创建、评审或管理合并请求
- 处理GitLab议题
- 监控或触发CI/CD流水线
- 克隆或管理仓库
- 从命令行执行任何GitLab操作
Prerequisites
前置条件
Verify glab installation before executing commands:
bash
glab --versionIf not installed, inform the user and provide platform-specific installation guidance.
执行命令前请确认glab已安装:
bash
glab --version若未安装,请告知用户并提供对应平台的安装指引。
Authentication Quick Start
认证快速入门
Most glab operations require authentication:
bash
undefined大多数glab操作需要认证:
bash
undefinedInteractive authentication
交互式认证
glab auth login
glab auth login
Check authentication status
检查认证状态
glab auth status
glab auth status
For self-hosted GitLab
针对自建GitLab实例
glab auth login --hostname gitlab.example.org
glab auth login --hostname gitlab.example.org
Using environment variables
使用环境变量
export GITLAB_TOKEN=your-token
export GITLAB_HOST=gitlab.example.org # for self-hosted
undefinedexport GITLAB_TOKEN=your-token
export GITLAB_HOST=gitlab.example.org # 适用于自建实例
undefinedCore Workflows
核心工作流
Creating a Merge Request
创建合并请求
bash
undefinedbash
undefined1. Ensure branch is pushed
1. 确保分支已推送
git push -u origin feature-branch
git push -u origin feature-branch
2. Create MR
2. 创建合并请求
glab mr create --title "Add feature" --description "Implements X"
glab mr create --title "Add feature" --description "Implements X"
With reviewers and labels
指定评审人和标签
glab mr create --title "Fix bug" --reviewer=alice,bob --label="bug,urgent"
undefinedglab mr create --title "Fix bug" --reviewer=alice,bob --label="bug,urgent"
undefinedReviewing Merge Requests
评审合并请求
bash
undefinedbash
undefined1. List MRs awaiting your review
1. 列出等待你评审的合并请求
glab mr list --reviewer=@me
glab mr list --reviewer=@me
2. Checkout MR locally to test
2. 拉取合并请求到本地测试
glab mr checkout <mr-number>
glab mr checkout <mr-number>
3. After testing, approve
3. 测试完成后批准合并请求
glab mr approve <mr-number>
glab mr approve <mr-number>
4. Add review comments
4. 添加评审评论
glab mr note <mr-number> -m "Please update tests"
undefinedglab mr note <mr-number> -m "Please update tests"
undefinedManaging Issues
管理议题
bash
undefinedbash
undefinedCreate issue with labels
创建带标签的议题
glab issue create --title "Bug in login" --label=bug
glab issue create --title "Bug in login" --label=bug
Link MR to issue
将合并请求关联到议题
glab mr create --title "Fix login" --description "Closes #<issue-number>"
glab mr create --title "Fix login" --description "Closes #<issue-number>"
List your assigned issues
列出分配给你的议题
glab issue list --assignee=@me
undefinedglab issue list --assignee=@me
undefinedMonitoring CI/CD
监控CI/CD流水线
bash
undefinedbash
undefinedWatch pipeline in progress
查看运行中的流水线
glab pipeline ci view
glab pipeline ci view
Check pipeline status
检查流水线状态
glab ci status
glab ci status
View logs if failed
查看失败流水线的日志
glab ci trace
glab ci trace
Retry failed pipeline
重试失败的流水线
glab ci retry
glab ci retry
Lint CI config before pushing
推送前校验CI配置
glab ci lint
undefinedglab ci lint
undefinedCommon Patterns
常见使用场景
Working Outside Repository Context
在仓库上下文外操作
When not in a Git repository, specify the repository:
bash
glab mr list -R owner/repo
glab issue list -R owner/repo当不在Git仓库目录下时,需指定仓库:
bash
glab mr list -R owner/repo
glab issue list -R owner/repoSelf-Hosted GitLab
自建GitLab实例
Set hostname for all commands:
bash
export GITLAB_HOST=gitlab.example.org为所有命令设置主机名:
bash
export GITLAB_HOST=gitlab.example.orgor per-command
或针对单个命令设置
glab repo clone gitlab.example.org/owner/repo
undefinedglab repo clone gitlab.example.org/owner/repo
undefinedListing Unresolved MR Comments
列出未解决的合并请求评论
bash
glab api "projects/:id/merge_requests/{mr}/discussions?per_page=100" | jq '[.[] | select(.notes[0].resolvable == true and .notes[0].resolved == false) | {id: .notes[0].id, body: .notes[0].body[0:100], path: .notes[0].position.new_path, line: .notes[0].position.new_line}]'bash
glab api "projects/:id/merge_requests/{mr}/discussions?per_page=100" | jq '[.[] | select(.notes[0].resolvable == true and .notes[0].resolved == false) | {id: .notes[0].id, body: .notes[0].body[0:100], path: .notes[0].position.new_path, line: .notes[0].position.new_line}]'Automation and Scripting
自动化与脚本编写
Use JSON output for parsing:
bash
glab mr list --output=json | jq '.[] | .title'使用JSON输出以便解析:
bash
glab mr list --output=json | jq '.[] | .title'Replying to MR Notes/Threads
回复合并请求的评论/线程
glab mr notebash
undefinedglab mr notebash
undefined1. Find the discussion_id containing the note
1. 找到包含目标评论的discussion_id
glab api "projects/:id/merge_requests/{mr}/discussions" | jq '.[] | select(.notes[].id == {note_id}) | .id'
glab api "projects/:id/merge_requests/{mr}/discussions" | jq '.[] | select(.notes[].id == {note_id}) | .id'
2. Post reply to the discussion thread
2. 向讨论线程推送回复
glab api --method POST "projects/:id/merge_requests/{mr}/discussions/{discussion_id}/notes" --field body="Your reply"
Example:
```bashglab api --method POST "projects/:id/merge_requests/{mr}/discussions/{discussion_id}/notes" --field body="Your reply"
示例:
```bashGet discussion_id for note 13698970
获取评论ID为13698970对应的discussion_id
glab api "projects/:id/merge_requests/1013/discussions" | jq '.[] | select(.notes[].id == 13698970) | {id}'
glab api "projects/:id/merge_requests/1013/discussions" | jq '.[] | select(.notes[].id == 13698970) | {id}'
Returns: {"id": "5356c3552e72e7b4c49276eb4dacfe3efe5c2c5c"}
返回结果:{"id": "5356c3552e72e7b4c49276eb4dacfe3efe5c2c5c"}
Reply to that thread
回复该线程
glab api --method POST "projects/:id/merge_requests/1013/discussions/5356c3552e72e7b4c49276eb4dacfe3efe5c2c5c/notes" --field body="Thanks for the review!"
undefinedglab api --method POST "projects/:id/merge_requests/1013/discussions/5356c3552e72e7b4c49276eb4dacfe3efe5c2c5c/notes" --field body="Thanks for the review!"
undefinedUsing the API Command
使用API命令
The command provides direct GitLab API access:
glab apibash
undefinedglab apibash
undefinedBasic API call
基础API调用
glab api projects/:id/merge_requests
glab api projects/:id/merge_requests
IMPORTANT: Pagination uses query parameters in URL, NOT flags
重要提示:分页需在URL中使用查询参数,而非标志位
❌ WRONG: glab api --per-page=100 projects/:id/jobs
❌ 错误用法:glab api --per-page=100 projects/:id/jobs
✓ CORRECT: glab api "projects/:id/jobs?per_page=100"
✓ 正确用法:glab api "projects/:id/jobs?per_page=100"
Auto-fetch all pages
自动获取所有分页内容
glab api --paginate "projects/:id/pipelines/123/jobs?per_page=100"
glab api --paginate "projects/:id/pipelines/123/jobs?per_page=100"
POST with data
发送POST请求并携带数据
glab api --method POST projects/:id/issues --field title="Bug" --field description="Details"
undefinedglab api --method POST projects/:id/issues --field title="Bug" --field description="Details"
undefinedBest Practices
最佳实践
- Verify authentication before executing commands:
glab auth status - Use to explore command options:
--helpglab <command> --help - Link MRs to issues using "Closes #123" in MR description
- Lint CI config before pushing:
glab ci lint - Check repository context when commands fail:
git remote -v
- 执行命令前验证认证状态:
glab auth status - 使用探索命令选项:
--helpglab <command> --help - 在合并请求描述中使用"Closes #123"关联议题
- 推送前校验CI配置:
glab ci lint - 命令失败时检查仓库上下文:
git remote -v
Common Commands Quick Reference
常用命令速查
Merge Requests:
- - Your assigned MRs
glab mr list --assignee=@me - - MRs for you to review
glab mr list --reviewer=@me - - Create new MR
glab mr create - - Test MR locally
glab mr checkout <number> - - Approve MR
glab mr approve <number> - - Merge approved MR
glab mr merge <number>
Issues:
- - List all issues
glab issue list - - Create new issue
glab issue create - - Close issue
glab issue close <number>
CI/CD:
- - Watch pipeline
glab pipeline ci view - - Check status
glab ci status - - Validate .gitlab-ci.yml
glab ci lint - - Retry failed pipeline
glab ci retry
Repository:
- - Clone repository
glab repo clone owner/repo - - View repo details
glab repo view - - Fork repository
glab repo fork
合并请求:
- - 查看分配给你的合并请求
glab mr list --assignee=@me - - 查看需要你评审的合并请求
glab mr list --reviewer=@me - - 创建新的合并请求
glab mr create - - 拉取合并请求到本地测试
glab mr checkout <number> - - 批准合并请求
glab mr approve <number> - - 合并已批准的合并请求
glab mr merge <number>
议题:
- - 列出所有议题
glab issue list - - 创建新议题
glab issue create - - 关闭议题
glab issue close <number>
CI/CD:
- - 查看流水线运行状态
glab pipeline ci view - - 检查流水线状态
glab ci status - - 校验.gitlab-ci.yml配置
glab ci lint - - 重试失败的流水线
glab ci retry
仓库:
- - 克隆仓库
glab repo clone owner/repo - - 查看仓库详情
glab repo view - - Fork仓库
glab repo fork
Progressive Disclosure
进阶参考
For detailed command documentation, refer to:
- references/commands-detailed.md - Comprehensive command reference with all flags and options
- references/quick-reference.md - Condensed command cheat sheet
- references/troubleshooting.md - Detailed error scenarios and solutions
Load these references when:
- User needs specific flag or option details
- Troubleshooting authentication or connection issues
- Working with advanced features (API, schedules, variables, etc.)
如需详细的命令文档,请参考:
- references/commands-detailed.md - 包含所有标志位和选项的完整命令参考
- references/quick-reference.md - 精简版命令速查表
- references/troubleshooting.md - 详细的错误场景及解决方案
在以下场景中可加载这些参考文档:
- 用户需要特定标志位或选项的详细信息
- 排查认证或连接问题
- 使用高级功能(API、调度、变量等)
Common Issues Quick Fixes
常见问题快速修复
"command not found: glab" - Install glab or verify PATH
"401 Unauthorized" - Run
glab auth login"404 Project Not Found" - Verify repository name and access permissions
"not a git repository" - Navigate to repo or use flag
-R owner/repo"source branch already has a merge request" - Use to find existing MR
glab mr listFor detailed troubleshooting, load references/troubleshooting.md.
"command not found: glab" - 安装glab或检查环境变量PATH配置
"401 Unauthorized" - 执行重新认证
glab auth login"404 Project Not Found" - 验证仓库名称及访问权限
"not a git repository" - 切换到仓库目录或使用标志位指定仓库
-R owner/repo"source branch already has a merge request" - 使用查找已存在的合并请求
glab mr list如需详细排查步骤,请加载references/troubleshooting.md。
Notes
注意事项
- glab auto-detects repository context from Git remote
- Most commands have flag to open in browser
--web - Use for scripting and automation
--output=json - Multiple GitLab accounts can be authenticated simultaneously
- Commands respect Git configuration and current repository context
- glab会自动从Git远程仓库检测上下文
- 大多数命令支持标志位,可在浏览器中打开对应页面
--web - 使用格式输出以便自动化脚本解析
--output=json - 可同时认证多个GitLab账号
- 命令会遵循Git配置及当前仓库上下文