fatal: not a git repository
This error occurs when you run a Git command outside of an initialized Git workspace or its subdirectories.
Usually happens because:
- ☑ Running commands outside a Git tracked directory
- ☑ Deleted or corrupt hidden .git metadata folder
- ☑ Active shell is in a parent/sibling folder
🔍 Quick Checklist:
What is fatal: not a git repository?
This error indicates that Git was executed in a directory that has not been initialized as a Git repository, and has no '.git' tracking folder in its directory hierarchy. When a Git command runs, the Git client searches the current directory and all parent directories recursively looking for the '.git' folder. If it reaches the root directory without finding one, it throws this exception and halts.
Common Causes
- No repository initialization: Running Git commands in a brand new folder before running 'git init'.
- Wrong working directory: Your command terminal is active in a parent folder or sibling directory outside the cloned repository.
- Deleted or corrupted .git directory: The hidden '.git' metadata folder was accidentally deleted or corrupted.
| Cause | Frequency |
|---|---|
| Command executed in wrong working directory | ⭐⭐⭐⭐⭐ |
| Brand new project directory without git init | ⭐⭐⭐⭐ |
| Accidentally deleted or missing .git folder | ⭐⭐ |
Common Mistakes
- Running global system commands inside the root directory or user directory by mistake instead of project subfolders.
- Deleting the hidden `.git` folder thinking it is garbage or temporary caching data, which breaks all local repository history.
How to Fix
Git Operations & Verification
Initialize the active directory to establish the Git database hierarchy and add files.
# 1. Initialize local Git database
$ git init
Initialized empty Git repository in /home/user/project/.git/
# 2. Verify status is active
$ git status
On branch main
No commits yetPlatform Specific Fixes
Look for hidden .git metadata directories in terminal files listing.
# Show hidden directories
ls -la | grep "\.git"Best Practices
- Always check your current working directory using `pwd` on Linux/macOS or `Get-Location` on Windows before running Git commands.
- Use terminal prompts that display the active Git branch name automatically to know when you are in a valid repository.
Frequently Asked Questions (FAQ)
Q: What does 'not a git repository' mean?
It means Git cannot track changes in your current directory because there is no '.git' directory present in the folder tree.
Q: How do I fix this error?
You can either navigate ('cd') into a folder that is already a Git repository, or initialize the current folder using the command 'git init'.
Q: Why did this error happen suddenly in a working project?
This can happen if you accidentally deleted the hidden '.git' folder in your project's root directory, or if you opened the terminal inside the wrong parent directory.
Q: Can I run Git commands on subdirectories?
Yes. As long as a parent folder contains the '.git' directory, you can run Git commands from any subdirectory inside the project.