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

GitHub Action中git log --skip命令返回空值问题求助

解决GitHub Action中git log --skip返回空的问题

核心原因

GitHub Actions的actions/checkout默认只拉取最近1个commit(fetch-depth: 1),本地仓库只有当前HEAD这一条提交记录,用--skip 1会跳过唯一的commit,自然返回空值。

解决方案1:拉取足够的提交历史

修改checkout步骤,设置fetch-depth为需要的深度:

  • 如果需要完整仓库历史,设为0:
- uses: actions/checkout@v4
  with:
    fetch-depth: 0
  • 只需要最近2个commit(满足--skip 1的需求),设为2更高效:
- uses: actions/checkout@v4
  with:
    fetch-depth: 2

解决方案2:改用更稳妥的commit引用(推荐)

不需要依赖--skip参数,直接用HEAD^获取上一个commit,即使fetch-depth为2也能正常工作:

- name: Catch commit changes
  run: |
    # 获取上一个commit的哈希值
    COMMITLOG=$(git rev-parse HEAD^)
    git diff --name-only $COMMITLOG..HEAD > commit_log_raw.txt

或者更简洁的写法,直接跳过变量:

- name: Catch commit changes
  run: git diff --name-only HEAD^..HEAD > commit_log_raw.txt

针对PR场景的补充

如果是PR触发的工作流,对比当前分支和目标分支的差异更合理,用以下命令替代:

- name: Catch PR changes
  run: git diff --name-only origin/${{ github.base_ref }}..HEAD > commit_log_raw.txt

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 10:32:37