Troubleshooting

fatal: not a git repository? Why Git cannot find .git and how to fix it

fatal: not a git repository means Git found no .git here or in any parent folder. Fix the folder, a broken .git, worktrees, mounts, cron and Docker.

Published ~8 min read
On this page

Short answer: Git looks for a .git folder in the current directory and then in every parent directory, and fatal: not a git repository (or any of the parent directories): .git means it found none. Either you are in the wrong folder (cd into the project, or run git -C /path/to/repo status), or the folder was never made a repository (git init or git clone), or its .git is broken.

bash · zsh
pwd                              # where am I?
ls -a                            # is there a .git here?
git rev-parse --show-toplevel    # top of the repository, if Git finds one
git -C /srv/app status           # run Git in another folder without cd

Every message on this page comes from Git 2.39.5 on Debian 12, run in throwaway repositories in a scratch folder. Git printed the full scratch path; we show it as /home/me/lab to keep the lines short. Every variant ends with exit status 128.

How Git finds a repository

Git has no list of your repositories. Each command starts in the current working directory and walks upward: ./.git, then ../.git, and so on up to /. The first valid .git wins, which is why git status works in src/lib deep inside a project. Three things in the git(1) manual change that walk:

  • GIT_DIR (or --git-dir) names the repository directly, and no search happens.
  • GIT_CEILING_DIRECTORIES lists directories Git should not move up into while searching.
  • By default Git does not cross a filesystem boundary; GIT_DISCOVERY_ACROSS_FILESYSTEM=true lifts that.
output · Git 2.39.5
$ cd /home/me/lab/empty && git status; echo "exit $?"
fatal: not a git repository (or any of the parent directories): .git
exit 128
$ cd /home/me/lab/app/src/lib && git rev-parse --show-toplevel
/home/me/lab/app
$ git rev-parse --show-prefix
src/lib/

git -C PATH runs the command as if Git had been started in PATH, so git -C /home/me/lab/app status works from anywhere, while git -C /home/me/lab/empty status prints the same fatal line.

Which message, which cause

Message (Git 2.39)CauseFix
(or any of the parent directories): .gitWrong folder, no git init or clone yet, cloned one level deeper, or a .git without HEADcd, git -C, clone, restore HEAD
not a git repository: '/nonexistent'GIT_DIR or --git-dir points at no repositoryunset GIT_DIR
not a git repository: /old/path/.git/worktrees/wt1A .git file points to a moved worktree or submodule directorygit worktree repair, git mv
invalid gitfile format: .../.gitThe .git is a file without a gitdir: lineDelete it or clone again
(or any parent up to mount point /)The search stopped at a filesystem boundarycd into the repo or set the variable
detected dubious ownership in repositoryA different error: the repository belongs to another usersafe.directory

Wrong folder, no repository, or one level too deep

The most common cause is simply the folder. A new terminal opens in your home directory, and a project unpacked from a zip archive has no .git at all. Run pwd and ls -a; if there is no .git here, look one level down. A clone creates its own folder named after the repository, so cloning inside an empty project folder puts the repository one level deeper than you expect:

output · Git 2.39.5
$ mkdir proj && cd proj && git clone -q /home/me/lab/app
$ ls -A
app
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ cd app && git rev-parse --show-toplevel
/home/me/lab/proj/app

Clone into the current folder with git clone URL . (it must be empty) if you want no extra level. If you do not remember where a project lives, find lists every repository below a folder; -prune keeps it out of the .git folders themselves, and the pattern also matches the .git files of worktrees and submodules:

bash
find ~ -name .git -prune 2>/dev/null

If the folder really has never been a repository, decide what it should be. git init starts a new, empty history. If the code belongs to an existing remote, clone that remote instead: running git init, committing the files and then pulling a remote that already has commits ends in refusing to merge unrelated histories. And if the shell answers git: command not found rather than this fatal line, Git itself is missing; see git: command not found.

A .git that is there but broken

Git accepts a .git directory only if it looks like a repository, which includes a HEAD file. When that file is missing, Git skips the folder, keeps searching upward and gives the same message as if there were no .git at all. An empty .git directory behaves the same. Pointing GIT_DIR at it shows the real problem:

output · Git 2.39.5
$ rm .git/HEAD
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ GIT_DIR=$PWD/.git git status
fatal: not a git repository: '/home/me/lab/broken/.git'
$ ls .git/refs/heads
main
$ echo 'ref: refs/heads/main' > .git/HEAD
$ git status
On branch main
nothing to commit, working tree clean

Write the branch that exists in .git/refs/heads (or in .git/packed-refs) into HEAD, then run git fsck. If objects are missing too, clone again and copy your uncommitted files over. A .git that is an empty file gives a different line: fatal: invalid gitfile format: /home/me/lab/emptyfile/.git. Such a file is usually left over from a copied worktree or submodule (next section).

A .git file that points to a moved worktree or submodule

In a linked worktree and in a submodule, .git is not a folder but a one-line file: gitdir: PATH. Move the folder that PATH names and the link breaks.

Worktrees

git worktree add writes an absolute path. When we moved the main repository, the worktree stopped working, and git worktree repair run from the main repository, with the worktree's path as its argument, fixed the link:

output · Git 2.39.5
$ cat wt1/.git
gitdir: /home/me/lab/main1/.git/worktrees/wt1
$ mv main1 main1-moved
$ git -C wt1 status
fatal: not a git repository: /home/me/lab/main1/.git/worktrees/wt1
$ git -C main1-moved worktree repair /home/me/lab/wt1
repair: .git file broken: /home/me/lab/wt1
$ git -C wt1 status | head -1
On branch feature

Submodules

A submodule's .git file holds a relative path, here gitdir: ../../.git/modules/vendor/lib. Copy the submodule folder out of its superproject and that path leads nowhere: fatal: not a git repository: /home/me/lab/libcopy/../../.git/modules/vendor/lib. To work on the library on its own, clone its URL separately. Inside the superproject, move a submodule with git mv vendor/lib third/lib (create third first), which kept it working in our test; a plain mv ended in fatal: cannot chdir to '../../../../vendor/lib'.

The opposite trap: after a plain git clone, submodule folders are empty, and Git commands there quietly act on the superproject, because the search walks up into it. git submodule update --init fills them.

or any parent up to mount point /

When the walk reaches a directory on another filesystem, Git stops and says so. We got this in /proc, which is its own filesystem; the path Git names is the parent it did not enter:

output · Git 2.39.5
$ cd /proc && git status
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
$ GIT_DISCOVERY_ACROSS_FILESYSTEM=1 git status
fatal: not a git repository (or any of the parent directories): .git

In real life this appears in a folder that is its own mount (a second disk, an NFS share, a Docker volume or bind mount) when the repository sits above the mount point. Usually you are simply in the wrong place: cd into the repository. If the layout is intended, set GIT_DISCOVERY_ACROSS_FILESYSTEM=1 for that shell. Note the second line above: crossing the boundary only helps when a repository really exists further up. GIT_CEILING_DIRECTORIES gives the plain message when it stops the search: with the ceiling set to app/src, git status in app/src/lib failed although app/.git was there.

Scripts, sudo, cron and systemd start somewhere else

A command that works in your terminal and fails from a script almost always runs in another directory. None of these start in your project folder:

  • sudo -i: per sudo(8) it changes to the target user's home directory before running the shell. Plain sudo git ... keeps your directory.
  • cron: Debian's cron changes into the crontab owner's HOME before it runs a job (the chdir call in do_command.c of the Debian cron source).
  • systemd services: per systemd.exec(5), WorkingDirectory= defaults to the root directory for system services and to the user's home for user services.

We reproduced it with two deploy scripts started from /: the one that calls git pull on its own fails, the one that names the repository works.

output · Git 2.39.5, run from /
$ cat deploy-bad.sh
#!/bin/sh
git pull --ff-only
$ ./deploy-bad.sh; echo "exit $?"
fatal: not a git repository (or any of the parent directories): .git
exit 128
$ cat deploy-good.sh
#!/bin/sh
set -eu
APP=/home/me/lab/shallow
git -C "$APP" pull --ff-only
$ ./deploy-good.sh; echo "exit $?"
Already up to date.
exit 0

On a VPS or dedicated server, write deploy scripts this way: either cd /srv/app || exit 1 as the first line, or git -C /srv/app on every Git call, and in a systemd unit set WorkingDirectory=/srv/app. If the script runs as another user, such as a deploy account, the next problem is usually ownership, not the folder; see dubious ownership.

Docker builds and CI pipelines

A RUN git rev-parse HEAD in a Dockerfile fails with the same message whenever .git is not inside the image. Docker's build context page explains the two usual reasons: .dockerignore excludes files from the context, and when you build from a Git URL, BuildKit does not keep the .git directory unless you pass --build-arg BUILDKIT_CONTEXT_KEEP_GIT_DIR=1. Even a context that includes .git only helps if the Dockerfile copies it in. The cleaner way is to read the commit on the host and hand it in as a build argument:

bash · Dockerfile
docker build --build-arg GIT_COMMIT="$(git rev-parse HEAD)" .
# in the Dockerfile:
ARG GIT_COMMIT
LABEL org.opencontainers.image.revision=$GIT_COMMIT

In GitHub Actions, steps that run before actions/checkout have no repository. The action's README also says that when Git 2.18 or newer is not on the PATH (a minimal job container, for example), it falls back to the REST API and downloads the files without a .git; install Git in the container to get a real clone. A shallow checkout (fetch-depth defaults to 1) is still a repository: our git clone --depth 1 answered git status normally. It can break commands that need history, such as git describe when the tag lies outside the fetched commits, but not repository discovery.

Not the same error: detected dubious ownership

People often search for both together, but here Git did find the repository and refuses to use it because another user owns it. This check arrived with the fix for CVE-2022-24765 in Git 2.35.2 (and the backports 2.30.3 to 2.34.2). Run as root on a repository owned by nobody:

output · Git 2.39.5
$ git status
fatal: detected dubious ownership in repository at '/home/me/lab/owned'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/me/lab/owned

The best fix is to run Git as the user who owns the files (sudo -u deploy git -C /srv/app pull) or to fix the ownership. If the sharing is intended, add the path to safe.directory in your global or system config, as the message says; the git-config manual notes that Git ignores it in a repository's own config. With Git 2.39.5 here, git -c safe.directory=PATH status also worked for one command. Avoid safe.directory=*, which switches the check off for every repository. Under sudo, Git as root also trusts repositories owned by the user in SUDO_UID, so sudo git ... in your own repository passes.

Quick checklist

  • pwd and ls -a: is there a .git here or in a parent?
  • git rev-parse --show-toplevel in the folder you expect to be the repository.
  • env | grep ^GIT_: a stale GIT_DIR or ceiling variable?
  • cat .git if it is a file: does the gitdir: path exist?
  • In scripts, cron and services: git -C /path or an explicit cd.

Once Git finds the repository again, the usual next step is a branch of your own: git create branch shows the commands.

Frequently asked

What does fatal: not a git repository (or any of the parent directories): .git mean?

Git searched the current folder and every parent folder for a valid .git directory and found none, so it stops with exit status 128. Run pwd and ls -a to check where you are, then cd into the project or run git -C /path/to/repo with the command. If the folder never was a repository, create one with git init or get it with git clone.

Why does git work in my terminal but not in cron or a systemd service?

Those run in another working directory: Debian's cron starts jobs in the user's home directory, and a system service starts in / unless WorkingDirectory= is set. Git then searches from there and finds no .git. Use git -C /srv/app in the script, cd into the repository first, or set WorkingDirectory= in the unit.

What does or any parent up to mount point / mean?

Git walked up to a filesystem boundary, such as a separate disk, an NFS share or a container volume, and by default it does not search past one. Usually you are in the wrong folder, so cd into the repository. If the repository really sits above the mount, set GIT_DISCOVERY_ACROSS_FILESYSTEM=1.

Is detected dubious ownership the same error?

No. With dubious ownership Git did find the repository but refuses to use it because another user owns the files, a check added in Git 2.35.2. Run Git as the owner, fix the ownership, or add the path with git config --global --add safe.directory PATH.

Related articles

All troubleshooting