Resources Infrastructure

How to Fix "fatal: not a git repository (or any of the parent directories): .git"

Why Git prints fatal: not a git repository, how to fix it in a terminal, an editor, a Docker build, or a CI job, how it differs from "not in a git directory" and "does not appear to be a git repository", and how to run Git commands in an automated workflow without hitting it.

TL;DR — fatal: not a git repository (or any of the parent directories): .git means Git found no .git directory in the current folder or any folder above it. Check where you are with pwd, then cd into the project folder. If the folder has no .git, run git clone <url> for an existing project or git init for a new one. In Docker and CI, the usual cause is a step that never received the .git directory.

Every Git command that reads history or branches needs a repository: git status, git pull, git log, git push all fail with this “not a git repository” error when run outside one. When you run one, Git does not assume the current folder is the project root: it looks for .git in the current folder, then in its parent, and so on up to /. If it reaches the top without finding one, it stops with this error.

$ cd /tmp && git status
fatal: not a git repository (or any of the parent directories): .git

How to fix it, in order

Most cases are solved by the first step. Work down the list until git status works.

1. Check where you are

The most common cause is a terminal opened one level too high, in your home folder or a workspace that contains several projects.

pwd
ls -a

If you see the project files but no .git, you are in the wrong folder or the folder is not a repository. Move into the project and confirm Git sees it:

cd ~/code/my-project
git rev-parse --show-toplevel # prints the repository root

In VS Code and other editors, the integrated terminal opens at the root of the opened folder. If you opened a parent folder that holds several repositories, open the repository folder itself, or cd into it in the terminal.

2. Clone or initialize the folder

A project downloaded as a ZIP archive or copied without hidden files has no .git directory. Clone it instead:

git clone https://github.com/example-org/my-project.git
cd my-project

For a new project that should become a repository:

git init
git add .
git commit -m "Initial commit"

3. Check Git environment variables

GIT_DIR tells Git where the repository is, bypassing the search. If a script or shell profile set it to a path that no longer exists, every command fails, with the path in the message:

env | grep ^GIT_
unset GIT_DIR GIT_WORK_TREE

GIT_CEILING_DIRECTORIES can also stop the upward search before it reaches your repository. See the Git environment variables reference.

4. Repair a submodule or worktree

In a submodule or a linked worktree, .git is a small file, not a directory. It contains a line such as gitdir: ../.git/modules/lib. If that path no longer exists, Git reports the error from inside the subfolder, with the missing path in the message.

For a submodule whose folder under .git/modules/ was deleted, reset it from the parent repository root:

git submodule deinit -f lib # use the submodule path
git submodule update --init lib

git submodule update --init on its own fails in this case with could not get a repository handle, which is why deinit -f comes first. A submodule that was never initialized does not cause the error: its folder is empty, so Git commands run there use the parent repository.

For a linked worktree whose main repository was moved, point Git to the worktree again from the main repository:

git worktree repair ../my-worktree # path to the linked worktree

Do not run git worktree prune to fix this. It leaves the broken link as it is, and for a worktree whose own folder was moved, it deletes the metadata that repair needs.

5. Repair a corrupted HEAD or a deleted .git

Git only treats a folder as a repository if .git/HEAD is valid. After a crash or a disk-full error, HEAD can end up empty, and every command reports fatal: not a git repository even though .git is there:

cat .git/HEAD # should print: ref: refs/heads/main
echo 'ref: refs/heads/main' > .git/HEAD # use your branch name
git status

If the .git directory itself was deleted, the local history is gone. Clone the repository again into a new folder and copy your uncommitted files over.

Errors that look similar

MessageWhat is wrongFix
fatal: not a git repository (or any of the parent directories): .gitNo .git in the current folder or its parentsMove into the project, clone, or git init
fatal: not a git repository: '/path/.git'GIT_DIR or a .git file points to a path that does not existunset GIT_DIR, or repair the submodule/worktree
fatal: not in a git directoryAn older Git ran git config with a repository-only option outside a repository; current Git prints --local can only be used inside a git repositoryRun it from inside the repository
fatal: 'origin' does not appear to be a git repositoryThe remote name or URL is wrong or missinggit remote -v, then git remote add origin <url> or git remote set-url origin <url>
fatal: detected dubious ownership in repositoryThe repository belongs to another user than the one running Gitgit config --global --add safe.directory <path>

The last one shows up mostly in containers and CI runners, where the user running Git is not the user who cloned the files. Git refuses to use the repository until the path is marked as safe; see safe.directory.

Why it happens in Docker builds and CI jobs

On a laptop, the error usually means the wrong folder. In automation, it usually means the job runs where the repository metadata never arrived:

  • Docker builds. .dockerignore files commonly exclude .git to keep the build context small. COPY . . then copies the code without the repository, and a RUN git rev-parse HEAD or RUN git describe --tags in the Dockerfile fails. Compute the value outside the build and pass it in: docker build --build-arg GIT_SHA=$(git rev-parse --short HEAD) .
  • CI steps without a checkout. Each job, and sometimes each step, starts in a fresh workspace. A step that runs Git commands needs its own checkout step, or must run in the folder where the checkout happened.
  • Artifacts copied between steps. A build output passed from one step to the next rarely includes .git. Run Git commands in the step that has the clone.

Ready to orchestrate your infrastructure?

Run Git commands in a Kestra flow

In Kestra, the Git Clone task fetches a repository and the next tasks run commands against it. Each task has its own working directory, so the clone and the commands that use it go inside a WorkingDirectory task, which gives them the same folder:

id: git_commands_in_a_flow
namespace: company.team
tasks:
- id: workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
tasks:
- id: clone
type: io.kestra.plugin.git.Clone
url: https://github.com/kestra-io/blueprints
branch: main
- id: inspect
type: io.kestra.plugin.scripts.shell.Commands
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: alpine/git
commands:
- git config --global --add safe.directory "$PWD"
- git rev-parse --show-toplevel
- git log -1 --format='%h %s'

What each part prevents:

  • No missing .git. Without WorkingDirectory, inspect would start in an empty folder and fail with fatal: not a git repository. Inside it, the .git directory created by clone is there.
  • No dubious ownership stop. The commands run in a container whose user can differ from the owner of the cloned files; the safe.directory line marks the folder as safe before any other Git command.
  • No credentials in the flow. For a private repository, add username and password: "{{ secret('GITHUB_TOKEN') }}" to the Clone task; the token stays in Kestra’s secret store and is masked in logs.

Clone fetches only the latest commit by default. Commands that need history or tags, such as git describe, need the depth property raised or a tag set on the Clone task.

To keep the flows themselves in a Git repository and deploy them from there, see version control and CI/CD.

Terraform, Ansible and Kubernetes, orchestrated from one place.

Frequently asked questions

Find answers to your questions right here, and don't hesitate to Contact Us if you couldn't find what you're looking for.