git “fatal: not a git repository (or any of the parent directories)”: how to fix it

Every git command first walks up from the current folder looking for a <code>.git</code> folder. If it reaches the top of the disk without finding one, you get this message. Nine times out of ten you’re simply one folder away from your project.

How git looks for a repository

Run git status in /home/sam/app/src/lib and git checks lib/.git, then src/.git, then app/.git, and so on up to /. The first one it finds is your repository. The message names .git because that’s what it never found.

pwd                              # where am I?
ls -a                            # is there a .git here?
git rev-parse --show-toplevel    # the repo root, once you're inside one

Causes and fixes

SituationFix
You cloned, but you’re still in the parent foldergit clone makes a new folder: cd my-repo
A new project that was never put under gitgit init, then git add . and git commit
Downloaded a ZIP from the websiteA ZIP has no history. Clone instead, or git init and add the remote
.git deleted or the folder copied without hidden filesClone again into a new folder and copy your changed files over; a fresh git init loses the link to the old history
Inside docker build.git is usually in .dockerignore (rightly). Pass the commit as a build argument: --build-arg GIT_SHA=$(git rev-parse HEAD)
In CI, before the checkout step ranMove the git command after actions/checkout or its equivalent, in the same job
A GIT_DIR variable left setecho $GIT_DIR; if it prints a path, unset GIT_DIR GIT_WORK_TREE
A submodule or worktree moved on diskIts .git is a small file pointing elsewhere: git submodule update --init from the parent, or git worktree repair
A script or cron jobIt runs in a different folder: use git -C /path/to/repo status or cd first

Close cousins

Related

Once it’s a repository again, pushing to a remote that already has commits can give refusing to merge unrelated histories or push rejected (non-fast-forward). Want to check that your copied files really match the originals? Compare them in the text diff tool or check hashes with the hash generator. · src refspec main does not match any

Git says detected dubious ownership? See dubious ownership explained. Cloned fine, but images and models are tiny text files? See Git LFS pointer files.

Questions

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

Git looked in the current folder and every folder above it for a .git folder and found none, so it doesn't know which repository you mean.

How do I fix fatal: not a git repository?

cd into the folder that contains the .git folder, usually the one git clone created. If the project was never a repository, run git init.

Why do I get not a git repository inside a Docker build?

The .git folder is normally excluded by .dockerignore and is not part of the build context. Pass the commit hash in as a build argument instead.

Will git init fix a repository whose .git folder was deleted?

It creates a new, empty history, so the link to the old commits is lost. Clone the repository again and copy your changed files across instead.

Published .

Try it

Related guides

Newest guides