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
| Situation | Fix |
|---|---|
| You cloned, but you’re still in the parent folder | git clone makes a new folder: cd my-repo |
| A new project that was never put under git | git init, then git add . and git commit |
| Downloaded a ZIP from the website | A ZIP has no history. Clone instead, or git init and add the remote |
.git deleted or the folder copied without hidden files | Clone 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 ran | Move the git command after actions/checkout or its equivalent, in the same job |
A GIT_DIR variable left set | echo $GIT_DIR; if it prints a path, unset GIT_DIR GIT_WORK_TREE |
| A submodule or worktree moved on disk | Its .git is a small file pointing elsewhere: git submodule update --init from the parent, or git worktree repair |
| A script or cron job | It runs in a different folder: use git -C /path/to/repo status or cd first |
Close cousins
- detected dubious ownership in repository: the repo exists but belongs to another user (common with
sudo, Docker volumes and shared drives). Fix the owner, or trust it once withgit config --global --add safe.directory /path/to/repo. - not a git repository: '/path/.git/worktrees/x': a worktree whose main repo moved;
git worktree pruneorrepair. - fatal: not a valid object name: 'main': you’re in a brand-new repo with no commits yet; make the first commit.
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 .