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
- Make autointegrate action idempotent (#34) This allows us to trigger it on other events (e.g. a cron) to update for new LLVM commits without constantly clobbering the auto-integrate branch. Also... — committed to google/llvm-bazel by GMNGeoffrey 4 years ago
- need recent git else repo is not cloned properly - https://github.com/actions/checkout/issues/363 — committed to phlummox-mirrors/joplin by deleted user 4 years ago
- https://github.com/actions/checkout/issues/363 Skipping submodules because they were downloaded previously — committed to fjtrujy/ps2sdk-ports by deleted user 4 years ago
- Skipping submodules because this issue https://github.com/actions/checkout/issues/363 And how they were downloaded previously it still should work — committed to fjtrujy/ps2sdk-ports by deleted user 4 years ago
- Skipping submodules because this issue https://github.com/actions/checkout/issues/363 And how they were downloaded previously it still should work — committed to fjtrujy/ps2sdk-ports by deleted user 4 years ago
- Try to solve #43 See https://github.com/actions/checkout/issues/363#issuecomment-1084349610 — committed to geocompx/geocompy by Robinlovelace 2 years ago
- Deploy commit: Try to solve #43 See https://github.com/actions/checkout/issues/363#issuecomment-1084349610 4c9ce79cd67f5b4730735633907a08b55f297c18 — committed to geocompx/geocompy by Robinlovelace 2 years ago
- fix: badness https://github.com/actions/checkout/issues/363 — committed to winny-/pre-commit-docker by winny- a year ago
- chore: change workflow once again discussions: https://github.com/JamesIves/github-pages-deploy-action/issues/1083, https://github.com/actions/checkout/issues/363 — committed to BakasaRus/computer-architecture-course by BakasaRus a year ago
- https://github.com/actions/checkout/issues/363\#issuecomment-750448152 — committed to DataDog/dd-trace-py by emmettbutler a year ago
- debugging release pipeline — committed to AgnostiqHQ/covalent-slurm-plugin by wjcunningham7 a year ago
- Added Git initialization for CI (https://github.com/actions/checkout/issues/363#issuecomment-1084349610) — committed to kaiharuto/Portfolio by kaiharuto 9 months ago
- CI + Added a fix(?) for "fatal: not in a git directory" -> https://github.com/actions/checkout/issues/363 — committed to LinuxOnARM/alarm-pkgbuilds by MaxineToTheStars 6 months ago
@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 theactions/checkout
step.So the basic idea for container workflows should be:
apt-get install git
/yum install git
/apk add git
/ …)git config --global --add safe.directory "$GITHUB_WORKSPACE"
once before any othergit
commands are used.actions/checkout
. If you need the full Git history with tags, etc, also usefetch-depth: 0
.git ...
commands.The workflow should look like this (using Alpine Linux container here as an example):
I’m also having this trouble, it’s infuriating. A simple command like
git status
fails when run after checkout v2.Some additional info:
After reading #335, removing this fixed it for me:
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:
🤔
Edit:
See https://github.com/actions/checkout/issues/325
Seeing the same. No
.git
directory is created. Checked with als -la
in the workflow to see what is there.https://github.com/HebaruSan/NetKAN/runs/1534015545?check_suite_focus=true
My workaround was replacing
uses: actions/checkout@v2
withuses: actions/checkout@v1
.Removing
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.
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:
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
andgit 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 tocd foo/bar
before I can a git command.But in the old V1, the checkout action leaves you in the
path
directoryThis 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:I don’t understand it either, but removing my custom container from the job helped in my case, thanks!