gitlab
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse the CLI for everything. The user's OAuth access token is
exported as ; reads it automatically (the token
is also accepted via and for tooling
compatibility). Default host is — for self-hosted set
or pass per command.
glab$GITLAB_TOKENglabGITLAB_ACCESS_TOKENOAUTH_TOKENgitlab.comGITLAB_HOST--hostname <host>glab --helpglab <subcommand> --help所有操作均使用 CLI完成。用户的OAuth访问令牌会导出为;会自动读取该令牌(为兼容工具,也可通过和传入令牌)。默认主机为 —— 对于自托管实例,可设置环境变量,或在每个命令中传入参数。
glab$GITLAB_TOKENglabGITLAB_ACCESS_TOKENOAUTH_TOKENgitlab.comGITLAB_HOST--hostname <host>glab --helpglab <subcommand> --helpTwo ways to call glab — prefer subcommands
调用glab的两种方式 —— 优先使用子命令
Style A: First-class subcommands — START HERE
方式A:一级子命令 —— 从这里开始
glab issueglab mrglab repoglab ciglab jobglab pipelineglab releaseglab snippetglab variableglab labelglab milestoneglab schedule--output jsonglab issueglab mrglab repoglab ciglab jobglab pipelineglab releaseglab snippetglab variableglab labelglab milestoneglab schedule--output jsonStyle B: Raw REST / GraphQL via glab api
glab api方式B:通过glab api
调用原生REST/GraphQL接口
glab apiglab api <path>glab api graphql -f query='…'- — override method (default
-X POST|PATCH|PUT|DELETE, becomesGETwhenPOST/--fieldis set).--raw-field - — magic-typed (literals
-f key=value/true/false/ integers become JSON types, leadingnullreads from a file).@filename - — same as
-F key=valuebut always treats the value as a string.-f - — auto-walk
--paginate.Link: rel="next" - — target a different GitLab host than default.
--hostname <host> - Path placeholders: when run inside a git checkout, /
:fullpath/:branchare auto-populated from the repo. From a generic shell, encode the path manually (see recipes).:user
使用调用REST接口,使用调用GraphQL接口。常用参数如下:
glab api <path>glab api graphql -f query='…'- —— 覆盖默认请求方法(默认为
-X POST|PATCH|PUT|DELETE,当设置GET/--field时自动变为--raw-field)。POST - —— 自动识别类型(布尔值
-f key=value/true、false、整数会转换为JSON对应类型,值以null开头时会读取文件内容)。@filename - —— 与
-F key=value功能类似,但始终将值视为字符串类型。-f - —— 自动遍历
--paginate分页链接。Link: rel="next" - —— 指定目标GitLab主机,覆盖默认设置。
--hostname <host> - 路径占位符:在Git仓库目录中执行命令时,/
:fullpath/:branch会自动从仓库中获取值。在通用Shell环境中,需手动编码路径(参考示例)。:user
Recipes
示例
List open issues on a project
列出项目中的未关闭议题
sh
glab issue list --repo OWNER/PROJECT --opened --output json
glab issue list --repo OWNER/PROJECT --assignee=@me --output jsonsh
glab issue list --repo OWNER/PROJECT --opened --output json
glab issue list --repo OWNER/PROJECT --assignee=@me --output jsonView an issue with comments
查看包含评论的议题
sh
glab issue view 42 --repo OWNER/PROJECT --comments
glab issue view 42 --repo OWNER/PROJECT --output jsonsh
glab issue view 42 --repo OWNER/PROJECT --comments
glab issue view 42 --repo OWNER/PROJECT --output jsonCreate / comment / close an issue
创建/评论/关闭议题
sh
glab issue create --repo OWNER/PROJECT --title "Bug: foo" --description "Repro steps…" --label bug
glab issue note 42 --repo OWNER/PROJECT --message "Acknowledged."
glab issue close 42 --repo OWNER/PROJECTsh
glab issue create --repo OWNER/PROJECT --title "Bug: foo" --description "复现步骤…" --label bug
glab issue note 42 --repo OWNER/PROJECT --message "已收到。"
glab issue close 42 --repo OWNER/PROJECTList MRs assigned to / authored by me
列出分配给我/由我创建的合并请求
sh
glab mr list --repo OWNER/PROJECT --assignee=@me --opened --output json
glab mr list --repo OWNER/PROJECT --author=@me --openedsh
glab mr list --repo OWNER/PROJECT --assignee=@me --opened --output json
glab mr list --repo OWNER/PROJECT --author=@me --openedView an MR with diff and CI pipeline
查看包含差异和CI流水线的合并请求
sh
glab mr view 99 --repo OWNER/PROJECT
glab mr diff 99 --repo OWNER/PROJECT
glab ci view --repo OWNER/PROJECT --branch <BRANCH>sh
glab mr view 99 --repo OWNER/PROJECT
glab mr diff 99 --repo OWNER/PROJECT
glab ci view --repo OWNER/PROJECT --branch <BRANCH>Approve / merge / leave a note on an MR
批准/合并/为合并请求添加评论
sh
glab mr approve 99 --repo OWNER/PROJECT
glab mr note 99 --repo OWNER/PROJECT --message "Looks good — ready when CI is green."
glab mr merge 99 --repo OWNER/PROJECT --squash --remove-source-branchsh
glab mr approve 99 --repo OWNER/PROJECT
glab mr note 99 --repo OWNER/PROJECT --message "看起来没问题 —— CI通过后即可合并。"
glab mr merge 99 --repo OWNER/PROJECT --squash --remove-source-branchRead a file from the default branch (raw bytes)
从默认分支读取文件(原始字节)
sh
undefinedsh
undefinedURL-encode the project path AND the file path because both contain '/'.
对项目路径和文件路径进行URL编码,因为两者都包含'/'。
PROJECT=$(printf '%s' 'OWNER/PROJECT' | jq -sRr @uri)
FILE=$(printf '%s' 'src/main.go' | jq -sRr @uri)
glab api "projects/${PROJECT}/repository/files/${FILE}/raw?ref=main"
undefinedPROJECT=$(printf '%s' 'OWNER/PROJECT' | jq -sRr @uri)
FILE=$(printf '%s' 'src/main.go' | jq -sRr @uri)
glab api "projects/${PROJECT}/repository/files/${FILE}/raw?ref=main"
undefinedList the latest pipelines on a branch
列出分支上最新的流水线
sh
glab ci list --repo OWNER/PROJECT --status running,success,failed
glab ci view --repo OWNER/PROJECT --branch main
glab ci trace --repo OWNER/PROJECT <JOB_ID> # stream a job logsh
glab ci list --repo OWNER/PROJECT --status running,success,failed
glab ci view --repo OWNER/PROJECT --branch main
glab ci trace --repo OWNER/PROJECT <JOB_ID> # 流式输出作业日志Search across a group's issues
在群组范围内搜索议题
sh
glab api "groups/GROUP_PATH_OR_ID/issues?state=opened&search=keyword" \
--paginate \
| jq '.[] | {iid, title, project: .references.full, web_url}'sh
glab api "groups/GROUP_PATH_OR_ID/issues?state=opened&search=keyword" \
--paginate \
| jq '.[] | {iid, title, project: .references.full, web_url}'GraphQL example: project metadata + open MR count
GraphQL示例:项目元数据 + 未关闭合并请求数量
sh
glab api graphql -f query='
query($path: ID!) {
project(fullPath: $path) {
name webUrl
mergeRequests(state: opened) { count }
}
}' -f path=OWNER/PROJECTsh
glab api graphql -f query='
query($path: ID!) {
project(fullPath: $path) {
name webUrl
mergeRequests(state: opened) { count }
}
}' -f path=OWNER/PROJECTNotes
注意事项
- accepts
--repo OWNER/PROJECT,OWNER/PROJECT, full HTTPS URL, or git URL. The project path goes verbatim (no URL encoding) forGROUP/SUBGROUP/PROJECT, but does need--repoencoding when used inside ajq @uripath.glab api - For self-hosted GitLab the user must have authorized the connection
with the right . A 404 on a project you know exists usually means the connection is pointing at gitlab.com when the project lives elsewhere — surface that hint to the user.
host - Many commands have an
glabflag that takes--output(default) ortext.jsonandglab issue listadditionally haveglab mr list(--output-format/details/ids) which is a separate, list-only formatter. Pass the long flagurlsto avoid the short-flag confusion (--output jsonvs-O).-F
- 参数支持
--repo OWNER/PROJECT、OWNER/PROJECT、完整HTTPS URL或Git URL。使用GROUP/SUBGROUP/PROJECT时,项目路径无需URL编码,但在--repo路径中使用时,需要通过glab api进行编码。jq @uri - 对于自托管GitLab实例,用户必须已授权对应的连接。若已知存在的项目返回404错误,通常意味着连接指向了gitlab.com,而实际项目在其他主机上 —— 需向用户提示这一点。
host - 许多命令都有
glab参数,支持--output(默认)或text格式。json和glab issue list还额外提供glab mr list参数(可选值--output-format/details/ids),这是仅针对列表的独立格式化选项。建议使用长参数urls以避免短参数混淆(--output json与-O)。-F