In Git, the “fatal: not a git repository” error occurs because you’re not inside a Git repository.
This happens due to 3 common reasons:
- You forgot to initialize a new repo with ‘git init‘.
- You forgot to change to the git repo folder.
- You’ve deleted the .git folder.
Making sure none of the above is happening to you is usually enough to fix the error.
If you can’t fix the error based on the above, make sure to read along. You’ll learn what makes a Git repo and how to ensure the “fatal: not a git repository” error doesn’t happen again.
What Makes a Git Repo?
When you run ‘git init‘ on a project folder, your project becomes a (local) Git repository under version control.
Behind the scenes, this happens thanks to an invisible .git folder that is generated in the project folder. It is the .git folder that makes the project a Git repo.
You can run ls .git in the command line on your Git project to see the contents of the .git folder.
There are some template files, logs, index files, and packages that make up the .git folder.
These files and subfolders are configured in a way that allows you to version your projects. To keep it short, we’re not going to dig deeper into these subfolders and files.
The source for the “fatal: not a git repository” is that you’re currently in a folder that has no .git folder. In other words, you’re not in a Git project folder by mistake.
How to Know If You’re in a Git Repo?
To know whether you’re inside a Git repo, check if the .git folder exists.
To do this, open up your command line window or terminal and run ls -laf to see hidden files and folders:
$ ls -laf
This command lists all the hidden folders in the current working directory.
Let’s run the ls -laf command in a correctly set up Git folder on my device:
In the above screenshot, the .git folder exists. This suggests the project is a Git repo. The git commands should not produce errors.
If you don’t see the .git folder, that’s the problem.
You can of course just run the pwd command to see what your present working directory is. If the path doesn’t match the Git project folder, you know you’re in the wrong place!
Error After Cloning a Repo
When you run git clone to clone a repository from Git, you need to remember to move the present working directory to the cloned folder! Use the cd (change directory) command to do this:
$ cd your-git-project-folder
Let me demonstrate what happens if you don’t.
Here’s an exampleProject I cloned from GitHub. I’m trying to run git status to see if git commands are working:
Because I’m not in the exampleProject folder with my Terminal window, the command fails.
It’s important to remember to change the present working directory to the cloned Git project directory:
As you can see, now the git commands work because we’re actually in the project folder.
This is the most common reason why you get errors when running git commands.
Corrupted HEAD File?
One less frequent issue that might cause the “fatal: not a git repository” error is if your HEAD file is corrupted.
The HEAD file is a file that lives inside the .git folder that’s responsible for versioning your project. The HEAD file has information that points GIt to the branch you’re working on. If the HEAD is empty or corrupted in some other way, you will see the “fatal: not a git repository” error even though it seems you’re in a valid Git repo.
You can check the contents of the HEAD file from your command line window by running:
$ cat .git/HEAD
This should show the branch you’re currently in.
For example, in my case, I’m on the main branch which is also what the cat .git/HEAD says:
$ cat .git/HEAD ref: refs/heads/main
If you see something unexpected or if the file is empty, it might be the root cause of the issue. To fix the problem, figure out your current branch (with the “git branch” command) and place the current branch name in the HEAD file.
Today you learned how to fix the “fatal: not a git repository” error.
To take home, most of the time this error happens because you’re not in a Git repository. This might be because you forgot to initialize a repo with “git init”. Alternatively, you may have cloned a directory, but forgot to change to the directory with “cd your-cloned-repo”.
Last but not least, the HEAD file might be corrupted. If this is the case, it might look like you’re in a Git folder (the .git folder is there, etc.) but because the information in the HEAD file isn’t right, it doesn’t recognize the repo as a Git repo.
Thanks for reading. Happy coding!