Skip to main content
DevOps Insightsguides & answers

Git · troubleshooting

fatal: not a git repository

Git looked in this directory and every parent for a .git and found none, so the command has no repository to work on. Usually you are in the wrong directory, or the files were copied without .git, as in many Docker builds and downloaded archives.

Reproduced on Git 2.50.1, . Where the text says “reproduced”, the behaviour was run on that version, not taken from documentation.

First published

New to this? Read what is actually happening first, then come back to the steps.

On this page · 8 sections

Before you start

  • A terminal in the place the command failed, or the CI or Docker step's log.

Run this first

step 1 of 1
git rev-parse --show-toplevel

Look for A path, or the fatal line

Prints the repository's root when Git finds one, and this error when it does not.

Work out which cause you have

A few questions to narrow the list. Every answer ends in a command that confirms or rules the cause out — this cannot see your cluster, so nothing here is a certainty until you have checked.

Narrow it down

0 answered · nothing is sent anywhere

Where did the command run?

Cause space

5 of 5 still possible

  • You are not inside the repositoryCommon
  • The files were copied without .git (Docker builds, archives, CI artifacts)Common
  • A CI step runs before the checkout, or outside itOccasional
  • A worktree, submodule or GIT_DIR points at a path that is goneOccasional
  • The directory is on another filesystem mounted inside the repositoryRare

Nothing ruled out yet. Answer the question above and the branches your answer eliminates will strike through here.

Or diagnose it manually

In this order. The first command usually contains the whole answer.

  1. Step 1

    git rev-parse --show-toplevel

    Prints the repository's root when Git finds one, and this error when it does not.

    Look for A path, or the fatal line

Every cause, and how to fix it

Ordered by how often each one turns out to be the answer.

You are not inside the repository

Common

The terminal, script or CI step is in a different folder: the parent of the clone, a home directory, or a step whose working directory changed.

Confirm

pwd; ls -a

No .git here; cd into the clone and try again

Fix

  • cd into the repository (the folder git clone created), or pass -C: git -C path/to/repo status.

The files were copied without .git (Docker builds, archives, CI artifacts)

Common

A .dockerignore that excludes .git, a download of the source as a zip or tarball, or a CI artifact contains the files but not the repository, so a build step that runs git rev-parse or git describe fails.

Confirm

ls -a; grep -nxE '/?\.git/?' .dockerignore

No .git in the copy, or .git listed in .dockerignore

Fix

  • Run git where the repository exists, outside the build, and pass the result in as a build argument. Declare ARG GIT_SHA in the Dockerfile, then build with: docker build --build-arg GIT_SHA="$(git rev-parse HEAD)" .
  • Keeping .git out of the build context is usually right: it is large and can contain things that should not end up in an image.

A CI step runs before the checkout, or outside it

Occasional

In GitHub Actions the repository is not in the workspace until actions/checkout runs, and a step with a different working-directory may not be inside the clone.

Confirm

pwd; git rev-parse --show-toplevel

The step's directory is not the checked-out repository

Fix

  • Run actions/checkout before any step that uses git, and make sure the step's working-directory is inside the checkout.

A worktree, submodule or GIT_DIR points at a path that is gone

Occasional

In a worktree or submodule, .git is a file naming the real repository's location. If that repository moved, or GIT_DIR is set to a wrong path, Git names the missing path.

Confirm

cat .git 2>/dev/null; echo "GIT_DIR=$GIT_DIR"

A gitdir: path or GIT_DIR value that does not exist

Fix

  • For a worktree, run git worktree repair PATH-TO-WORKTREE from the main repository's new location.
  • For GIT_DIR, unset it: unset GIT_DIR.
git worktree repair ../wt

Run in the moved main repository; reproduced on Git 2.50.1.

The directory is on another filesystem mounted inside the repository

Rare

Git searches upward for .git but, by default, stops where one filesystem ends and another begins, such as a mounted volume or disk inside the repository. The message then says up to mount point.

Confirm

df -P . | awk 'NR==2 {print $NF}'

A mount point inside your repository: that is the boundary Git stopped at

Fix

  • Run git from a directory on the repository's own filesystem, or start it there with git -C /path/to/repo.
  • If the mounted directory is meant to be part of the working tree, let Git cross the boundary for that command or job: GIT_DISCOVERY_ACROSS_FILESYSTEM=1.
GIT_DISCOVERY_ACROSS_FILESYSTEM=1 git status

Reproduced on Git 2.50.1 inside a disk image mounted in a repository.

Understanding it properly

Skip this if you are mid-incident — the working part of the page is above. Worth reading afterwards, because understanding the mechanism is what stops the next one.

In plain terms

A repository is a folder with a hidden .git inside it. Git commands find it by looking in the current folder, then its parent, and so on up. A copy of the files without that .git, or a terminal in a different folder, leaves Git with nothing to read.

Words used on this page

.git
The directory holding the repository's history and settings. In a worktree or submodule it is a small file pointing at the real one.
Build context
The files Docker sends to the build. A .dockerignore can leave .git out of it.
GIT_DIR
An environment variable that tells Git exactly where the repository is, skipping the search.
What is actually happening

Reproduced on Git 2.50.1: git status in a folder with no repository above it printed fatal: not a git repository (or any of the parent directories): .git and exited 128. So did a copy of a repository exported with git archive, which contains the files and no .git, the same situation as a Docker build whose context leaves .git out.

With GIT_DIR pointing at a missing directory, the message names that path instead: fatal: not a git repository: '/nonexistent'.

A worktree whose main repository had been moved failed with the old path, because its .git file still pointed there. git worktree repair, run in the moved repository with the worktree's path, fixed it.

Git stops searching at a filesystem boundary unless GIT_DISCOVERY_ACROSS_FILESYSTEM is set; its source then says not a git repository (or any parent up to mount point …) and Stopping at filesystem boundary. Reproduced on Git 2.50.1 by mounting a small disk image inside a repository: git status inside the mount printed fatal: not a git repository (or any parent up to mount point …) and Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)., exit 128. The path it names is where the search stopped, the repository directory just above the mount. With GIT_DISCOVERY_ACROSS_FILESYSTEM=1 the same command found the repository.

How to tell this is your problem
WhereWhat you see
Any git commandfatal: not a git repository (or any of the parent directories): .git
GIT_DIR, a worktree or submodulefatal: not a git repository: PATH
Docker build or CI stepThe same line from a RUN step or script that calls git rev-parse or git describe

How to know it is actually fixed

  • git status shows a branch name instead of the fatal line.

Stopping it happening again

  • In Docker builds, pass commit information as build arguments rather than running git inside the build.
  • In scripts, use git -C "$REPO" instead of relying on the current directory.

Sources

Behaviour described here is drawn from official documentation. Where a figure could not be confirmed on an official page it is attributed in the text rather than stated as canonical.

Did this get you to an answer?

No text box on purpose — please do not paste production logs anywhere

Something wrong or out of date? Report it