How to fix fatal: not a git repository (or any of the parent directories): .git in Git

GitBEGINNERLOWUpdated Dec 6, 2025· 345 views

This error occurs when you run a Git command outside of a Git repository. Git cannot find a .git directory in the current folder or any parent folder. You need to either navigate to an existing repository, initialize a new one with git init, or clone an existing repository.

Quick fix

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

pwd
  1. Check your current directory
  2. Initialize a new Git repository
  3. Clone an existing repository
  4. Recover a deleted .git directory
git — error
pwd
fatal: not a git repository (or any of the parent directories): .git

What this error means

The "fatal: not a git repository (or any of the parent directories): .git" error indicates that Git cannot find a valid repository in your current working directory or any of its parent directories. When you run any Git command, Git searches for a `.git` folder starting from your current directory and moving up through parent directories until it finds one or reaches the filesystem root. This is one of the most common Git errors, especially for beginners. It simply means you're trying to use Git commands in a directory that isn't under version control. This error commonly occurs in several scenarios: - **Wrong directory**: You've navigated to the wrong folder and aren't in your project directory. - **Not initialized**: You created a new project folder but forgot to run `git init`. - **Cloning issues**: You intended to clone a repository but forgot to do so, or the clone failed. - **Deleted .git folder**: The `.git` directory was accidentally deleted or excluded. - **Nested terminal sessions**: Your terminal opened in a different directory than expected (common in IDEs). - **Submodule issues**: You're inside a submodule that hasn't been initialized. The error is straightforward to fix once you understand that Git needs a `.git` directory to function.

Typical symptoms

  • Any git command fails with 'fatal: not a git repository'
  • git status, git add, git commit, and other commands don't work
  • Error appears immediately when running any git command
  • Terminal shows the error regardless of which git command you try
  • IDE's Git integration shows errors or doesn't detect the repository
  • git log shows no history because there's no repository

Common causes

  1. Current directory is not inside any Git repository
  2. Git was never initialized in this project (forgot to run git init)
  3. You're in the wrong directory - not inside your project folder
  4. The .git directory was accidentally deleted
  5. Terminal or IDE opened in a different directory than your project
  6. Project was copied without the .git folder
  7. Repository was not cloned - directory was created manually instead
  8. Git submodule exists but was not initialized
  9. Working in a new terminal session that started in home directory

How to fix "fatal: not a git repository (or any of the parent directories): .git"

1Check your current directory

First, verify where you are in the filesystem:

bash
pwd

This shows your current working directory. Compare it to where your project should be located.

List directory contents to confirm:

bash
ls -la

Look for:
- Your project files (source code, package.json, etc.)
- A .git directory (hidden, shown with -a flag)

If you don't see your project files, you're in the wrong directory. Navigate to your project:

bash
cd /path/to/your/project

Common locations to check:

bash
# Home directory projects
cd ~/projects/my-project

# Desktop
cd ~/Desktop/my-project

# Documents
cd ~/Documents/my-project
2Initialize a new Git repository

If you're in the correct project directory but Git was never initialized, create a new repository:

bash
git init

This creates a new .git directory and initializes an empty Git repository.

Verify it worked:

bash
git status

You should see output like:

bash
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        your-files-here

Next steps after initializing:

bash
# Add all files to staging
git add .

# Create first commit
git commit -m "Initial commit"

# Add remote origin (if pushing to GitHub/GitLab)
git remote add origin git@github.com:username/repo.git

# Push to remote
git push -u origin main
3Clone an existing repository

If your project exists on GitHub, GitLab, or another remote server, clone it instead of initializing:

Clone via SSH (recommended):

bash
git clone git@github.com:username/repository-name.git
cd repository-name

Clone via HTTPS:

bash
git clone https://github.com/username/repository-name.git
cd repository-name

Important: After cloning, you must cd into the cloned directory. The clone command creates a new folder with the repository name.

Clone to a specific directory:

bash
git clone git@github.com:username/repository-name.git my-folder-name
cd my-folder-name

Clone to current directory (if empty):

bash
git clone git@github.com:username/repository-name.git .

Verify the clone worked:

bash
git status
git log --oneline -5
4Recover a deleted .git directory

If the .git folder was accidentally deleted but you have a remote copy, you can recover:

Option 1: Re-clone alongside your files

bash
# Backup your current files
cp -r . ../my-project-backup

# Clone fresh into a temp directory
git clone git@github.com:username/repo.git temp-repo

# Move the .git folder to your project
mv temp-repo/.git .

# Clean up
rm -rf temp-repo

# Check status - your changes should show as modifications
git status

Option 2: Re-initialize and force push (use with caution)
If you don't need the remote history:

bash
git init
git add .
git commit -m "Reinitialize repository"
git remote add origin git@github.com:username/repo.git
git push -f origin main  # WARNING: This overwrites remote history

Option 3: Fetch remote history into new repo

bash
git init
git remote add origin git@github.com:username/repo.git
git fetch origin
git reset --mixed origin/main  # Keeps your local changes
git status  # Shows what differs from remote
5Handle submodule issues

If you're working with Git submodules, you may need to initialize them:

Check if you're in a submodule directory:

bash
# Go to the parent project root
cd ..
git status

If this works, you were in a submodule that needs initialization.

Initialize all submodules:

bash
git submodule init
git submodule update

Or clone with submodules from the start:

bash
git clone --recurse-submodules git@github.com:username/repo.git

Update existing clone to include submodules:

bash
git submodule update --init --recursive

Check submodule status:

bash
git submodule status

If a submodule shows a - prefix, it's not initialized. If it shows a + prefix, it's at a different commit than recorded.

6Fix IDE or terminal working directory issues

IDEs and terminals sometimes open in unexpected directories:

VS Code:
- Check the folder open in VS Code (look at Explorer sidebar)
- Use File > Open Folder to open your project directory
- Use the integrated terminal which opens in the workspace root
- Check if you have a multi-root workspace open

Terminal:

bash
# See where you are
pwd

# Navigate to project
cd ~/path/to/project

# Optionally, add an alias to your shell config
# ~/.bashrc or ~/.zshrc
alias myproject="cd ~/projects/my-project"

IntelliJ/WebStorm:
- Check the Project view shows the correct root
- Use Terminal tab which opens in project root
- File > Open should point to the directory containing .git

Common issue with new terminal tabs:
New terminal tabs/windows often open in your home directory. Either:
1. Navigate to your project each time
2. Configure your terminal to remember the last directory
3. Use IDE's integrated terminal which opens in the project root

7Find nearby Git repositories

If you're not sure where your Git repositories are located, search for them:

Find all .git directories under your home folder:

bash
find ~ -name ".git" -type d 2>/dev/null

Find .git directories in common locations:

bash
find ~/projects ~/Documents ~/Desktop -name ".git" -type d 2>/dev/null

Search for a specific project:

bash
find ~ -type d -name "my-project-name" 2>/dev/null

Use locate (faster, but requires updatedb):

bash
locate -i "my-project-name"

On macOS, use Spotlight:

bash
mdfind "kMDItemFSName == 'my-project-name' && kMDItemContentType == 'public.folder'"

Once you find the correct directory, navigate to it and verify:

bash
cd /path/to/found/project
git status

Advanced notes

Understanding Git's repository detection:

Git searches for a repository by looking for a .git directory in the current folder and all parent folders up to the filesystem root. This is why you can run Git commands from any subdirectory within a repository.

The search order is:
1. Current directory (./)
2. Parent directory (../)
3. Grandparent directory (../../)
4. Continue up to filesystem root (/)

Environment variables affecting repository detection:

bash
# Override the working directory
GIT_WORK_TREE=/path/to/worktree git status

# Override the .git directory location
GIT_DIR=/path/to/.git git status

# Useful for scripts operating on multiple repos
GIT_DIR=/project/.git GIT_WORK_TREE=/project git log

Bare repositories:

Bare repositories (used for servers) don't have a working directory:

bash
# Clone as bare repo (for servers)
git clone --bare https://github.com/user/repo.git

# Regular git commands won't work in bare repos
# Use git with explicit options instead
git --bare log

The .git file for worktrees and submodules:

In worktrees and submodules, .git is a file, not a directory:

bash
# .git file content points to actual git directory
cat .git
# Output: gitdir: /path/to/main/repo/.git/worktrees/branch-name

Detecting repository programmatically:

bash
# Check if inside a Git repository
git rev-parse --is-inside-work-tree 2>/dev/null && echo "In a repo" || echo "Not in a repo"

# Get the repository root directory
git rev-parse --show-toplevel

# Get the .git directory path
git rev-parse --git-dir

Safe directory configuration (Git 2.35.2+):

Git may refuse to operate in repositories owned by other users:

bash
# Add a directory to the safe list
git config --global --add safe.directory /path/to/repo

# Or trust all repositories (less secure)
git config --global --add safe.directory '*'

Debugging repository detection:

bash
# Verbose output showing what Git is checking
GIT_TRACE=1 git status

# Show the current directory and .git location
git rev-parse --show-toplevel --git-dir

Frequently asked questions

What does "fatal: not a git repository (or any of the parent directories): .git" mean?
git" error indicates that Git cannot find a valid repository in your current working directory or any of its parent directories. git folder starting from your current directory and moving up through parent directories until it finds one or reaches the filesystem root. This is one of the most common Git errors, especially for beginners.
How do I fix "fatal: not a git repository (or any of the parent directories): .git"?
This error occurs when you run a Git command outside of a Git repository. Git cannot find a .git directory in the current folder or any parent folder. You need to either navigate to an existing repository, initialize a new one with git init, or clone an existing repository.
What causes "fatal: not a git repository (or any of the parent directories): .git"?
Current directory is not inside any Git repository Git was never initialized in this project (forgot to run git init) You're in the wrong directory - not inside your project folder
Is the "fatal: not a git repository (or any of the parent directories): .git" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading