Skip to main content

Git · 5 common causes

fatal: not a git repository

Updated

The directory you ran the command in — and none of its parents — contains a Git repository.

The exact message

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

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is fatal: not a git repository.

How to fix it

Cause 1 of 5

You are in the wrong directory.

pwd to see where you are and ls -a to look for a .git entry. The repository is whichever folder contains it; cd there. This is by far the most common cause and costs nothing to check first.

Cause 2 of 5

The project was never turned into a repository.

git init if it is genuinely new. If it should already be a clone, you probably downloaded a ZIP of the code — a ZIP contains no history, so clone the repository instead.

Cause 3 of 5

You cloned and stayed in the parent folder.

git clone <url> creates a subdirectory named after the repository. cd project and the same command will work.

Cause 4 of 5

The .git file in a submodule or worktree points at a path that no longer exists.

In a submodule or a linked worktree, .git is a file containing a gitdir: line rather than a directory. Read it with cat .git; if that path is gone, run git submodule update --init from the parent repository to restore the link.

Cause 5 of 5

A CI step or container runs Git without a checkout.

Some pipelines skip the checkout for cached jobs, and some Docker builds copy source without the .git directory — which is often deliberate, since it keeps history out of the image. Move the Git command to a stage that has the repository, or pass the value it needed (a commit SHA, a version) in as a build argument.

Stopping it coming back

Show the current Git branch in your shell prompt: it disappears the moment you step outside a repository, which makes this error obvious before you run the command.

fatal: not a git repository — common questions

How far up does Git search for .git?
All the way to the filesystem root by default, stopping early if GIT_CEILING_DIRECTORIES is set. So if the message appears, there is no repository anywhere above you either.
Is it safe to run git init to make the error go away?
Only if the directory is meant to be a new repository. Running it inside a folder that should have been a clone creates a second, unrelated history — which is the usual road to 'refusing to merge unrelated histories' later.
Why does VS Code say the folder is not a repository when the terminal disagrees?
The editor opened a different folder than your terminal is in — typically a subdirectory or a parent. Open the folder that contains .git, or add it through Source Control so both agree.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor