Error Encyclopedia

fatal: Not a Git Repository Error

Fix 'fatal: not a git repository' errors when running git commands outside a git repository or in a detached work tree.

What Does This Error Mean?

The 'fatal: not a git repository' error means git cannot find a .git directory in the current or parent directory. Git commands must be run inside an initialized git repository.

Common Causes

1

Running git commands in a directory that is not a git repository

2

The .git directory was deleted or corrupted

3

Working in a subdirectory of a repo that has a broken .git file

4

Git submodule not initialized

5

The current directory is outside the repository boundaries

6

Detached work tree without proper GIT_DIR configuration

How to Fix It

Initialize a new repository

Use git init to create a new git repository in the current directory.

# Initialize a new repository
git init

# Or clone an existing repository
git clone https://github.com/user/repo.git
cd repo

Find the nearest repository

Check if you are inside a repository and find its root.

# Check if .git exists
ls -la .git

# Find the root of the current repository
git rev-parse --show-toplevel

# Move to the repository root
cd "$(git rev-parse --show-toplevel)"

Initialize submodules if needed

If working with submodules, they must be initialized.

# Clone repo with submodules
git clone --recurse-submodules https://github.com/user/repo.git

# Or initialize submodules after cloning
git submodule init
git submodule update

# If submodule .git is a file (not directory), run:
git submodule update --init --recursive

Related Tools

Use these tools to debug and fix this error:

Related Errors

Other common errors in this category: