How to Fix "fatal: not a git repository" in Git
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!
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:
- Verify your current directory. You can use the
pwd
(Print Working Directory) command to see where you are. - Navigate to the correct directory. Use the
cd
command to move into your project folder. - Check for the
.git
folder. Once you're in the right place, you can runls -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!
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.