How To Fix "fatal: not a git repository" in Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
• 2 minutes to read

Sometimes, a developer may encounter the fatal: not a git repository error when running any git command. This is a very common error but is luckily easily fixable.

This article will go through the causes and solutions for the fatal: not a git repository (or any of the parent directories): .git error.

Let's get to it 😎.

fatal not a git repository

Why does the fatal: not a git repository error happen?

The fatal: not a git repository error happens because you are not inside a Git repository.

Here are a few reasons why this error happens:

  • You are inside the wrong working directory.
  • You didn't initialize the Git repository.
  • Your HEAD file is corrupted.

Luckily, each of those reasons is easily fixable.

How to fix the fatal: not a git repository error?

To fix this error, you need to go through the list of reasons, find the one that causes the error by using the elimination method and finally fix it.

Here is the process to follow:

1. Ensure you are inside the correct directory that contains the Git repository.

2. Verify that the Git repository has been initialized by checking if a .git folder exists.

To check if this folder exists, use the ls -la command.

If the .git folder does not exist in the correct directory, initialize the Git repository using the git init command.

bashgit init

3. Verify that the HEAD file is not corrupted.

bash> cat .git/HEAD
ref: refs/heads/main

If this command outputs something unexpected, find the branch you are working on and put it in your HEAD file.

One of those fixes should do the job and fix the error.

Final thoughts

As you can see, multiple reasons can cause this error.

Most of the time, however, it happens because you are not in the correct working directory.

git fatal not a got repository

Here are some other Git tutorials for you to enjoy:

Comments (3)
Reply to:
Christian June 22, 2023 19:01PM
Would just like to note that it seems I also managed to get "fatal: not a git repository" when was the culprit. Using the below command solved the issue for me. git config --global --add
dennis May 16, 2023 15:47PM
error: object file .git/objects/83/422d9214d97c7e951a396c20acd42caba03840 is emptyerror: object file .git/objects/83/422d9214d97c7e951a396c20acd42caba03840 is emptyfatal: loose object 83422d9214d97c7e951a396c20acd42caba03840 (stored in .git/objects/83/422d9214d97c7e951a396c20acd42caba03840) is corrupt
Alain Mazy March 18, 2023 07:22AM
Moi, j'ai beau tout essayer, je n'arrive pas à installer reactide .. Pourtant j'ai chargé le zip, je l'ai décompressé dans un dossier c:\\Reactide\\ j'ai installé Git que j'ai ajouté aux variable d'environement, je me positionne dans le rep Reactide, mais aucune des commandes git ou npm ne fonctionne.