You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让`git fetch --depth=1 origin +refs/tags/*:refs/tags/*`无标签时不报错

GitHub Actions拉取标签无远端标签不报错的解决方案

核心需求

CI环境中需要拉取远端标签校验当前提交是否有关联标签,使用actions/checkout默认配置仅拉取单提交无标签信息,拉取标签命令在远端无标签时不能报错终止,需继续执行后续git describe校验逻辑。

可用解决方案

方案1:Shell层忽略错误(全Git版本兼容)

在拉取标签命令末尾追加|| true,强制命令返回0退出码,CI不会判定步骤失败:

git fetch --depth=1 origin +refs/tags/*:refs/tags/* || true

该方案兼容性最强,不受Git版本限制,适合所有CI环境。

方案2:Git原生参数控制(Git 2.31+ 支持)

使用Git原生提供的--no-fail-on-empty参数,当远端无匹配标签可拉取时自动返回成功状态:

git fetch --depth=1 --no-fail-on-empty origin +refs/tags/*:refs/tags/*

该方案更符合Git操作规范,无额外Shell逻辑依赖。

效果说明

两种方案都不会对后续git describe --all --exact-match命令的执行逻辑产生影响:

  • 存在匹配标签时,命令正常返回标签信息,退出码为0
  • 无匹配标签时,命令正常返回非0退出码,可直接用该状态作为是否有关联标签的判断依据

完整Actions步骤示例

steps:
  - name: 检出代码
    uses: actions/checkout@v2
  - name: 拉取远端标签
    run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* || true
  - name: 校验提交关联标签
    run: git describe --all --exact-match

内容的提问来源于stack exchange,提问作者Jens Meindertsma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 04:54:03