πŸ’ΈSave up to $132K/month in CI costs!πŸ‘‰ Try Free✨
Skip to main content
← Back to workflows

How to Checking out a repo in GitHub Actions?

actions/checkout -
GitHub Action
v4.1.5
5,775
Contributors
Contributor - ericscipleContributor - thboop
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'Usage of Checkout V4 GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

OpenCommit logo

Checkout V4

Action for checking out a repo


What is Checkout V4 GitHub Action?​

This action lets us check out our repository into the $GITHUB_WORKSPACE, which means all our workflow scripts can access the repo directly. By default, it only pulls the latest commit that triggered the workflow, but you can tweak this to grab the whole history by setting fetch-depth: 0.

How to fetch only the root files with Checkout V4 GitHub Action?​

To fetch only the root files of your repository using the Checkout V4 GitHub Action, you use the sparse-checkout parameter set to .. This configuration tells the action to limit the checkout to the root files only, excluding any directories or subdirectories.

- uses: actions/checkout@v4
with:
sparse-checkout: .

How to fetch only the root files and .github and src folder with Checkout V4 GitHub Action?​

To specifically fetch the root files along with the .github and src folders, you adjust the sparse-checkout parameter to include these directories. This setup ensures that the action only checks out these specified paths, optimizing the checkout process by ignoring other files and folders.

- uses: actions/checkout@v4
with:
sparse-checkout: |
.github
src

How to fetch only a single file?​

When you need to fetch only a single file, such as README.md, you specify this file in the sparse-checkout parameter. The sparse-checkout-cone-mode set to false ensures that no additional pattern matching optimizations are applied, focusing the checkout strictly on the listed file.

- uses: actions/checkout@v4
with:
sparse-checkout: |
README.md
sparse-checkout-cone-mode: false

How to fetch all history for all tags and branches with Checkout V4 GitHub Action?​

Setting fetch-depth: 0 configures the Checkout V4 action to fetch all commits across all branches and tags. This is useful for workflows that require a complete history of the repository.

- uses: actions/checkout@v4
with:
fetch-depth: 0

How to checkout a different branch?​

To checkout a specific branch, use the ref parameter and specify the branch name. This allows the action to directly checkout the desired branch, bypassing the default branch.

- uses: actions/checkout@v4
with:
ref: my-branch

How to checkout HEAD^ with Checkout V4 GitHub Action?​

To checkout the commit before the latest, you first set fetch-depth: 2 to fetch at least the last two commits. Then, use a run command to checkout HEAD^, which refers to the previous commit.

- uses: actions/checkout@v4
with:
fetch-depth: 2
- run: git checkout HEAD^

How to checkout multiple repos (side by side)?​

To checkout multiple repositories side by side, specify different paths for each repo within the same workflow. This allows each checked-out repository to be stored in a separate directory under the workspace.

- name: Checkout
uses: actions/checkout@v4
with:
path: main

- name: Checkout tools repo
uses: actions/checkout@v4
with:
repository: my-org/my-tools
path: my-tools

How to checkout multiple repos (nested) with Checkout V4 GitHub Action?​

Similar to the side-by-side checkout, but here you can nest one repository inside another by specifying the paths accordingly. This setup is useful for managing dependencies or plugins managed in separate repositories.

- name: Checkout
uses: actions/checkout@v4

- name: Checkout tools repo
uses: actions/checkout@v4
with:
repository: my-org/my-tools
path: my-tools

How to checkout multiple repos (private)?​

When dealing with private repositories, use a Personal Access Token (PAT) to authenticate. Specify the repository and path, and include the token to gain access.

- name: Checkout
uses: actions/checkout@v4
with:
path: main

- name: Checkout private tools
uses: actions/checkout@v4
with:
repository: my-org/my-private-tools
token: ${{ secrets.GH_PAT }}
path: my-tools

How to checkout pull request HEAD commit instead of merge commit?​

To checkout the exact HEAD commit of a pull request instead of the merge commit, use the pull request's head.sha in the ref parameter.

- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

How checkout pull request on closed event?​

This configuration triggers the checkout process when a pull request is opened, synchronized, or closed

, ensuring that the action responds to changes in pull request status.

on:
pull_request:
branches: [main]
types: [opened, synchronize, closed]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

How to push a commit using the built-in token?​

This setup enables automatic commit generation and pushing using GitHub Actions' built-in authentication. It’s perfect for automated updates or procedural commits.

on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
date > generated.txt
git config user.name github-actions
git config user.email [email protected]
git add .
git commit -m "generated"
git push

These examples cover a variety of use cases to help you utilize the Checkout V4 action effectively in your workflows.