checkout: fatal: not a git repository when using checkout@v2

I am trying to run git commands in an action using checkout@v2 but I am getting the error

fatal: not a git repository

My yaml looks like the following

name: name
on: [push, pull_request]
jobs: 
  some_job:
    name: some_name
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: some_name
         run: |
           git status

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 33
  • Comments: 24

Commits related to this issue

Most upvoted comments

@jorgesolebur Try git config --global --add safe.directory $(realpath .) before any other git operations.

For those of you using container images in their workflows, remember that many smaller container images do not have Git installed by default. So actions/checkout cannot use Git in those cases, unless you install it before the actions/checkout step.

So the basic idea for container workflows should be:

  1. Install Git. (apt-get install git / yum install git / apk add git / …)
  2. Inside the container run git config --global --add safe.directory "$GITHUB_WORKSPACE" once before any other git commands are used.
  3. Run actions/checkout. If you need the full Git history with tags, etc, also use fetch-depth: 0.
  4. Run your git ... commands.

The workflow should look like this (using Alpine Linux container here as an example):

name: Alpine
on: push
jobs:
  alpine:
    runs-on: ubuntu-latest
    container: alpine:latest
    steps:
      # Git is required so that actions/checkout does a proper Git checkout
      # instead of using the GitHub REST API.
      - name: Install Git in container
        run: |
          apk update
          apk add git
          # one time fix to avoid permission problems later on
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
      # Checks-out the repository under $GITHUB_WORKSPACE.
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      # Now the git commands can be used.
      - run: |
          git status
          git log -3
          ...

I’m also having this trouble, it’s infuriating. A simple command like git status fails when run after checkout v2.

Some additional info:

  • I’m using a custom built docker image, which is really just ubuntu-latest plus some apt-gets
  • My git version (confirmed by running git --version directly before git status) is 2.37.2
  • I’m definitely IN a git repo, I can see literally everything from my repo plus meta info like a .git folder full of stuff, including HEAD

After reading #335, removing this fixed it for me:

       container:
           image: ubuntu:latest

Somehow this was causing the wrong version of git to run. I don’t pretend to understand why.

I’m also seeing this when using self-hosted runners on Windows Server. No .git directory is created. This causes all sorts of issues.


Edit:

Ah, I see there is a subtle warning message:

The repository will be downloaded using the GitHub REST API
To create a local Git repository instead, add Git 2.18 or higher to the PATH

🤔


Edit:

See https://github.com/actions/checkout/issues/325

Seeing the same. No .git directory is created. Checked with a ls -la in the workflow to see what is there.

https://github.com/HebaruSan/NetKAN/runs/1534015545?check_suite_focus=true

total 168
drwxr-xr-x 5 1001  116   4096 Dec 10 23:14 .
drwxr-xr-x 6 root root   4096 Dec 10 23:14 ..
drwxrwxr-x 3 root root   4096 Dec 10 23:05 .github
-rw-rw-r-- 1 root root     50 Dec 10 23:05 .gitignore
-rw-rw-r-- 1 root root    210 Dec 10 23:05 .travis.yml
-rw-rw-r-- 1 root root   6502 Dec 10 23:05 LICENSE.md
drwxrwxr-x 3 root root 135168 Dec 10 23:05 NetKAN
-rw-rw-r-- 1 root root   1241 Dec 10 23:05 README.md
drwxrwxr-x 2 root root   4096 Dec 10 23:05 bin

My workaround was replacing uses: actions/checkout@v2 with uses: actions/checkout@v1.

Removing

       container:
           image: …

isn’t solution here. If you remove this code, workflow will be run on your self-hosted machine itself, not in the container. It seems to work because on your machine you must have newer version of Git.

Ah, I see there is a subtle warning message:

The repository will be downloaded using the GitHub REST API
To create a local Git repository instead, add Git 2.18 or higher to the PATH

🤔

Edit:

See #325

according to this warning Git version should be higher than 2.18. So the real solution is to upgrade Git inside of your container’s image or, if version is already equal or higher than 2.18, add it to the PATH. Also there is another dirty workaround - after actions/checkout@v2 step run git init:

name: name
on: [push, pull_request]
jobs: 
  some_job:
    name: some_name
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - run: git init
      - name: some_name
         run: |
           git status 

but this initialize empty repository - git commands will work, but there will be no information about remote repository.

What worked for me was, installing git in the container, fetch-depth: 1 and git config --global --add safe.directory "$GITHUB_WORKSPACE"

I think there are several issues being discussed in this issue. Some people do not have git installed in their runner, some people have older versions of git. My issue was neither of those. My issue was that I expected v3 to behave the same as v1 and they do not.

Below is a snippet that shows that with V3, it clones the source into a subdir foo/bar. Then in the next step, I have to cd foo/bar before I can a git command.

     - uses: actions/checkout@v3
        with:
          path: foo/bar
      - run: |
          cd foo/bar
          git status

But in the old V1, the checkout action leaves you in the path directory

      - uses: actions/checkout@v1
        with:
          path: foo/bar
      - run: |
          #  cd foo/bar   
          git status

This was the issue for me.

Just as an update, this error bugged me for quite a while just now. After testing all the suggestions here the only one that helped in the end was running this before my git commands:

git config --global --add safe.directory "$GITHUB_WORKSPACE"

After reading #335, removing this fixed it for me:

       container:
           image: ubuntu:latest

Somehow this was causing the wrong version of git to run. I don’t pretend to understand why.

I don’t understand it either, but removing my custom container from the job helped in my case, thanks!