gitgithubversion-controlgithub-actionsgit-fetch

Why does actions/checkout fetch the latest commit right after cloning the repository?


While trying to make sense of the action actions/checkout@version, I came across this answer in which the following was stated to explain what happens when the same action is executed:

The default steps being executed are:

  1. The current repo in which the workflow is being triggered gets cloned.

  2. Depending on the defined events such as a push or pull request:
    For a push event, it runs the command below, where $GITHUB_REF points to the latest commit on the specified branch for the push event in the workflow:
    git fetch --depth 1 $GITHUB_REF

What I don't understand pertains to the reason the latest commit is fetched after the repository have just been cloned and thus is already up to date.


Solution

  • The first step mentioned in your quote is incorrect (Emphasis mine):

    1. The current repo in which the workflow is being triggered gets cloned.

    Having looked at the code [GitHub] for the action here are the steps it performs to checkout the reference:

    1. If git is not present download the code using the API and return
    2. Otherwise if git is present initialize an empty git repository
    3. Add a remote for that empty repository pointing to the repository to be used
    4. Fetch & checkout the specific ref needed

    You can note here that the repository isn't actually cloned and rather than that a remote is configured on the newly initialized empty repository. Hence running fetch is really needed since it doesn't yet actually have the commit.

    Note: I've skipped some of the other steps that the code performs that aren't relevant to the question.