gitlab

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Use the
glab
CLI for everything. The user's OAuth access token is exported as
$GITLAB_TOKEN
;
glab
reads it automatically (the token is also accepted via
GITLAB_ACCESS_TOKEN
and
OAUTH_TOKEN
for tooling compatibility). Default host is
gitlab.com
— for self-hosted set
GITLAB_HOST
or pass
--hostname <host>
per command.
glab --help
and
glab <subcommand> --help
are always current.
所有操作均使用
glab
CLI完成。用户的OAuth访问令牌会导出为
$GITLAB_TOKEN
glab
会自动读取该令牌(为兼容工具,也可通过
GITLAB_ACCESS_TOKEN
OAUTH_TOKEN
传入令牌)。默认主机为
gitlab.com
—— 对于自托管实例,可设置
GITLAB_HOST
环境变量,或在每个命令中传入
--hostname <host>
参数。
glab --help
glab <subcommand> --help
始终为最新帮助文档。

Two ways to call glab — prefer subcommands

调用glab的两种方式 —— 优先使用子命令

Style A: First-class subcommands — START HERE

方式A:一级子命令 —— 从这里开始

glab issue
,
glab mr
,
glab repo
,
glab ci
,
glab job
,
glab pipeline
,
glab release
,
glab snippet
,
glab variable
,
glab label
,
glab milestone
,
glab schedule
. These print formatted text by default and JSON via
--output json
.
glab issue
glab mr
glab repo
glab ci
glab job
glab pipeline
glab release
glab snippet
glab variable
glab label
glab milestone
glab schedule
。这些命令默认输出格式化文本,通过
--output json
参数可输出JSON格式。

Style B: Raw REST / GraphQL via
glab api

方式B:通过
glab api
调用原生REST/GraphQL接口

glab api <path>
for REST,
glab api graphql -f query='…'
for GraphQL. Notable flags:
  • -X POST|PATCH|PUT|DELETE
    — override method (default
    GET
    , becomes
    POST
    when
    --field
    /
    --raw-field
    is set).
  • -f key=value
    — magic-typed (literals
    true
    /
    false
    /
    null
    / integers become JSON types, leading
    @filename
    reads from a file).
  • -F key=value
    — same as
    -f
    but always treats the value as a string.
  • --paginate
    — auto-walk
    Link: rel="next"
    .
  • --hostname <host>
    — target a different GitLab host than default.
  • Path placeholders: when run inside a git checkout,
    :fullpath
    /
    :branch
    /
    :user
    are auto-populated from the repo. From a generic shell, encode the path manually (see recipes).
使用
glab api <path>
调用REST接口,使用
glab api graphql -f query='…'
调用GraphQL接口。常用参数如下:
  • -X POST|PATCH|PUT|DELETE
    —— 覆盖默认请求方法(默认为
    GET
    ,当设置
    --field
    /
    --raw-field
    时自动变为
    POST
    )。
  • -f key=value
    —— 自动识别类型(布尔值
    true
    /
    false
    null
    、整数会转换为JSON对应类型,值以
    @filename
    开头时会读取文件内容)。
  • -F key=value
    —— 与
    -f
    功能类似,但始终将值视为字符串类型。
  • --paginate
    —— 自动遍历
    Link: rel="next"
    分页链接。
  • --hostname <host>
    —— 指定目标GitLab主机,覆盖默认设置。
  • 路径占位符:在Git仓库目录中执行命令时,
    :fullpath
    /
    :branch
    /
    :user
    会自动从仓库中获取值。在通用Shell环境中,需手动编码路径(参考示例)。

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 json
sh
glab issue list --repo OWNER/PROJECT --opened --output json
glab issue list --repo OWNER/PROJECT --assignee=@me --output json

View an issue with comments

查看包含评论的议题

sh
glab issue view 42 --repo OWNER/PROJECT --comments
glab issue view 42 --repo OWNER/PROJECT --output json
sh
glab issue view 42 --repo OWNER/PROJECT --comments
glab issue view 42 --repo OWNER/PROJECT --output json

Create / 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/PROJECT
sh
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/PROJECT

List 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 --opened
sh
glab mr list --repo OWNER/PROJECT --assignee=@me --opened --output json
glab mr list --repo OWNER/PROJECT --author=@me --opened

View 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-branch
sh
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-branch

Read a file from the default branch (raw bytes)

从默认分支读取文件(原始字节)

sh
undefined
sh
undefined

URL-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"
undefined
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"
undefined

List 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 log
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>     # 流式输出作业日志

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/PROJECT
sh
glab api graphql -f query='
  query($path: ID!) {
    project(fullPath: $path) {
      name webUrl
      mergeRequests(state: opened) { count }
    }
  }' -f path=OWNER/PROJECT

Notes

注意事项

  • --repo OWNER/PROJECT
    accepts
    OWNER/PROJECT
    ,
    GROUP/SUBGROUP/PROJECT
    , full HTTPS URL, or git URL. The project path goes verbatim (no URL encoding) for
    --repo
    , but does need
    jq @uri
    encoding when used inside a
    glab api
    path.
  • For self-hosted GitLab the user must have authorized the connection with the right
    host
    . 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.
  • Many
    glab
    commands have an
    --output
    flag that takes
    text
    (default) or
    json
    .
    glab issue list
    and
    glab mr list
    additionally have
    --output-format
    (
    details
    /
    ids
    /
    urls
    ) which is a separate, list-only formatter. Pass the long flag
    --output json
    to avoid the short-flag confusion (
    -O
    vs
    -F
    ).
  • --repo OWNER/PROJECT
    参数支持
    OWNER/PROJECT
    GROUP/SUBGROUP/PROJECT
    、完整HTTPS URL或Git URL。使用
    --repo
    时,项目路径无需URL编码,但在
    glab api
    路径中使用时,需要通过
    jq @uri
    进行编码。
  • 对于自托管GitLab实例,用户必须已授权对应
    host
    的连接。若已知存在的项目返回404错误,通常意味着连接指向了gitlab.com,而实际项目在其他主机上 —— 需向用户提示这一点。
  • 许多
    glab
    命令都有
    --output
    参数,支持
    text
    (默认)或
    json
    格式。
    glab issue list
    glab mr list
    还额外提供
    --output-format
    参数(可选值
    details
    /
    ids
    /
    urls
    ),这是仅针对列表的独立格式化选项。建议使用长参数
    --output json
    以避免短参数混淆(
    -O
    -F
    )。