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

GitHub Actions推送事件中,如何用actions/checkout获取指定提交历史?

在GitHub Actions Push事件中高效拉取目标提交历史

目前actions/checkout确实没有原生的from-ref这类参数,但可以通过结合Git原生命令,直接利用Push事件提供的before和after SHA值,精准拉取所需历史,无需提前统计提交数量。

实现方案

jobs:
  process-push-commits:
    runs-on: ubuntu-latest
    steps:
      - name: 初始化仓库并拉取当前推送的最新提交
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.after }}
          fetch-depth: 1  # 仅拉取最新提交,最小化初始拉取量

      - name: 拉取推送前的最后一个提交及完整推送历史
        run: |
          # 将推送前的提交拉取到本地远程分支引用
          git fetch origin ${{ github.event.before }}:refs/remotes/origin/pre-push
          # 拉取推送前提交到当前提交之间的所有历史(包含推送的所有提交)
          git fetch origin ${{ github.event.before }}..${{ github.event.after }}

原理说明

  • ${{ github.event.before }}:Push事件发生前,仓库HEAD的完整SHA值(即推送前的最后一个提交)
  • ${{ github.event.after }}:Push事件完成后,仓库HEAD的完整SHA值(即推送后的最新提交)
  • 通过git fetch origin <before>..<after>命令,Git只会拉取这两个SHA之间的所有提交历史,不会拉取仓库的完整历史,完全符合需求。

验证方法

执行以下命令可确认历史是否正确获取:

git log --oneline --graph ${{ github.event.before }}..HEAD

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:52:42