Skip to content
ohsudo, Home
CI/CD

Git "fatal: not a git repository (or any of the parent directories)": where is .git?

Fix Git "fatal: not a git repository (or any of the parent directories): .git": go to the right folder, clone or init, and handle Docker, CI, submodules and mounts.

Beginner3 min readApplies to Git 2.x · Linux · macOS · Windows · Docker · GitHub Actions · GitLab CI

by OhSudo ·

Checked on real software: Git, Ubuntu 24.04 · November 2026How we check

Part of a learning path:Git for everyday work

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

Git looks for a .git folder in the current directory, then in each parent up to /. It found none, so as far as Git knows, you’re not inside a repository. Usually you’re one folder away from it.

TL;DR

Terminal window
pwd
ls -a # is .git here?
cd ~/projects/shop # the folder you cloned into
git rev-parse --show-toplevel # works anywhere inside a repository

1. You’re in the wrong folder

The most common case: you cloned and stayed in the parent folder. git clone https://github.com/acme/shop.git creates shop/, and the repository is inside it:

Terminal window
cd shop
git status

ls -a shows the hidden .git folder, which marks the top of a repository. Inside any subfolder of it, git rev-parse --show-toplevel prints that top folder. Try it in the terminal, which starts one folder above a repository.

2. There’s no repository yet

For a new project, create one:

Terminal window
git init
git add .
git commit -m "First commit"

If you downloaded a project as a ZIP file instead of cloning it, it has no .git and no history. Clone it to get both, or run git init to start fresh.

3. In Docker builds

.dockerignore usually excludes .git (and it should, to keep the image small). Then a RUN git rev-parse HEAD in the Dockerfile fails with this error. Pass the information as a build argument instead:

Terminal window
docker build --build-arg GIT_COMMIT=$(git rev-parse --short HEAD) -t app .
ARG GIT_COMMIT=unknown
ENV GIT_COMMIT=$GIT_COMMIT

The same goes for tools that read Git during a build, like version plugins: give them the version as a variable.

4. In CI

  • GitHub Actions: without actions/checkout, the job’s folder is empty. Add - uses: actions/checkout@v5 before steps that use Git. Steps that run in another working-directory, or in a container, need the repository there too.
  • Shallow clones (fetch-depth: 1) are repositories too. They give other errors, such as missing tags or history, not this one.
  • Running as another user: in a container, if the repository belongs to another user, Git reports dubious ownership instead.

5. Less common cases

  • A mount point: Git doesn’t search past the filesystem it started on. In a folder under a mounted disk, such as /mnt/sub with a disk at /mnt, it stops when the next parent is on another filesystem, and names it:

    fatal: not a git repository (or any parent up to mount point /)
    Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

    Go into the repository itself, or set GIT_DISCOVERY_ACROSS_FILESYSTEM=1.

  • A submodule has a .git file that points into the parent repository. Moved or copied alone, it points nowhere: fatal: not a git repository: ../.git/modules/lib. Run git submodule update --init from the parent.

  • GIT_DIR set in your environment overrides the search. echo $GIT_DIR, and unset GIT_DIR if it’s left over.

Warning

If .git was deleted, the history is gone with it, and the files are only a copy of the last state. git init makes a new, empty history. If the project exists on a remote, clone it again into a new folder, and copy over the files you changed.

Shell, we have a problem!

Similar, but different: detected dubious ownership means the repository is there, owned by another user. src refspec main does not match any means a repository with no commits, or another branch name. fatal: '…' does not appear to be a git repository comes from git push or git pull, about the remote: check git remote -v. For everyday commands, see the Git cheat sheet.

$ ./practice.sh

Practice this in the terminal

Open the Terminal Playground with this article’s scenario loaded. It is safe and runs in your browser.

Open the terminal →

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles