Skip to main content

How to Fix "fatal: not a git repository" in Git

Short answer

This error means Git cannot find a .git directory in the current directory or any of its parent directories, so there is no repository for the command to operate on. The most common cause is simply being in the wrong directory — verify your location with pwd and then cd to the correct project folder. If you expect a .git folder to exist but don't see one, run ls -la to check for hidden directories; if it truly is absent, the directory was never initialised or the .git folder was accidentally deleted. To create a new repository in the current directory, run git init; to get a copy of an existing repository, run git clone <url>, which creates the .git folder automatically. Running Git commands in your home directory or a parent folder outside the project is a very common cause of this error, especially in shell sessions that have not been navigated to the right path.

You're ready to start working on your project, you type a Git command, and... bam! You get this message:

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

Don't worry! This is one of the most common errors a Git user will see, and it's usually very simple to resolve.

What Does This Error Mean?

In a nutshell, this error means you are trying to run a Git command in a folder that isn't a Git repository. Git works by creating a special hidden directory named .git at the root of your project. This .git folder contains all the history, commits, and configuration for your repository.

When you run a command like git status or git log, Git looks for this .git directory in the current folder. If it can't find one, it walks up through the parent directories until it either finds a .git folder or reaches the root of your filesystem. If it can't find a .git folder anywhere in the hierarchy, it gives you the "fatal: not a git repository" error.

The Git Cheat Sheet

No need to remember all Git commands and parameters: get our popular "Git Cheat Sheet" - for free!

Free. We email you a ZIP with our cheat sheets.

How to Fix It

There are two main reasons why you might be seeing this error. Let's walk through them.

Scenario 1: You're in the Wrong Directory

This is the most frequent cause. You might have simply forgotten to cd into your project's directory in the terminal.

Here's what you should do to fix this:

  1. Verify your current directory. You can use the pwd (Print Working Directory) command to see where you are.
  2. Navigate to the correct directory. Use the cd command to move into your project folder.
  3. Check for the .git folder. Once you're in the right place, you can run ls -la to see all files, including hidden ones. You should see the .git directory listed there.
# Am I in the right place?
$ pwd
/Users/your-user/Documents

# Oops, no. Let me go to my project folder.
$ cd /Users/your-user/Sites/my-cool-project

# Let's check if the .git directory is here...
$ ls -la
drwxr-xr-x  13 user  staff   416 Jul  1 10:20 .
drwxr-xr-x   5 user  staff   160 Jul  1 10:19 ..
drwxr-xr-x   8 user  staff   256 Jul  1 10:22 .git
-rw-r--r--   1 user  staff   123 Jul  1 10:20 README.md

# Great! Now Git commands will work.
$ git status

Scenario 2: The Project Isn't a Git Repository Yet

If you're starting a brand new project from scratch, or if you have an existing project that has never been tracked with Git, then there's no .git directory to find.

To fix this, you need to initialize a new Git repository in your project's root folder.

# Navigate to your project's root directory
$ cd /path/to/your/project

# Initialize a new Git repository
$ git init
Initialized empty Git repository in /path/to/your/project/.git/

This command creates the .git directory, and you can now start using Git commands to track your project's files. For a more detailed guide, check out our article on how to create a Git repository.

Alternatively, if the project already exists on a remote hosting service like GitHub, you should use the git clone command instead of git init. This will download the remote repository and automatically set up the .git directory for you.

# Clone the remote repository to your local machine
$ git clone https://github.com/user/repo.git
Cloning into 'repo'...
...
$ cd repo
$ git status

As you can see, this error is a simple bump in the road. It's just Git's way of telling you it's lost. By either navigating to the correct directory or initializing a new repository, you'll be back on track in no time!

Free download

Download the Cheat Sheet

Get 18 of our most popular Cheat Sheets in one handy ZIP!