[GitHub Actions](EN) Fetch more than one commit using fetch-depth


Environment and Prerequisite

  • GitHub Actions


Background

  • While using GitHub Actions, I needed to load past commit history in a job, so I looked into it and decided to write about it.


GitHub Actions Checkout fetch-depth

Usage

  • By default, only one commit is fetched (depth = 1). If you want to retrieve more commits, use the option below.
- uses: actions/checkout@v4
  with:
    # Number of commits to fetch. 0 indicates all history for all branches and tags.
    # Default: 1
    fetch-depth: 0


Example

Workflow File

  • Set fetch-depth to 5 and fetch five commits.
name: Test Git Log Workflow

on:
  workflow_dispatch:
    inputs:
      CHECKOUT_REF:
        required: false
        type: string
        default: main

jobs:
  print_git_commit_log_without_fetch_depth:
    name: job name 1
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repository
      uses: actions/checkout@v4
    - name: Print Commit Hash
      run: echo $GITHUB_SHA
    - name: Print Git Log
      run: git log
  print_git_commit_log_with_fetch_depth:
    name: job name 2
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repository
      with:
        fetch-depth: 5 # fetch recent five commits
      uses: actions/checkout@v4
    - name: Print Commit Hash
      run: echo $GITHUB_SHA
    - name: Print Git Log
      run: git log

Result

  • As you can see from the result below, it fetched 5 commits, not just one.
  git log
  shell: /usr/bin/bash -e {0}
commit 5d9dfa3701670b5ac034829d2e421202b9eebc11
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:57:08 2025 +0900

    Update test-git-log.yml
  git log
  shell: /usr/bin/bash -e {0}
commit 5d9dfa3701670b5ac034829d2e421202b9eebc11
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:57:08 2025 +0900

    Update test-git-log.yml

commit 78bbeaf925e2f4ec0d17c32d84d7cebcfcce0fcf
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:55:07 2025 +0900

    Update and rename test-git-log to test-git-log.yml

commit 2de68ba14ebe93aa444b701350ae4a44f81d9c5b
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:54:02 2025 +0900

    Update test-git-log

commit 17bc45af78267dfc634b30d24358c2c878b660f8
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:52:48 2025 +0900

    Create test-git-log

commit 9c9a2a3e7c58bfa02c4c024186757a721d40e9ee
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Fri Nov 15 18:22:07 2024 +0900

    Merge pull request #55 from TWpower/workflow-complete-event-release-7cef35c
    
    release(devops): Update Workflow Complete Event Workflows


Opinion

  • While setting up CI/CD, there was a code that fetched past commits, but it kept throwing errors, which led me to look into it. Thinking about it, fetching everything could be wasteful, so it makes sense to have an option like this. Just like the concept of pagination, there are cases where it’s better not to load everything at once.


Reference