fatal: not a git repository (or any of the parent directories) โ Fix
fatal: not a git repository (or any of the parent directories): .git
This error means Git canโt find a .git folder in your current directory or any parent directory. Every git command โ git status, git commit, git push, git log โ requires being inside a repository. Without the .git folder, Git doesnโt know what repo youโre working in.
How Git finds your repository
When you run any git command, Git looks for a .git directory starting from your current working directory and walking up the tree:
/home/user/projects/my-app/src/components/ โ you are here
/home/user/projects/my-app/src/
/home/user/projects/my-app/ โ .git found here โ
/home/user/projects/
/home/user/
/home/
/
If Git reaches the filesystem root (/) without finding .git, it throws the โfatal: not a git repositoryโ error.
Fix 1: Youโre in the wrong directory
The most common cause. You opened a terminal, ran a git command, but youโre not inside your project folder.
# Check where you are
pwd
# /home/user โ not in a project!
# Navigate to your project
cd ~/projects/my-app
# Verify it's a repo
git status
# On branch main, nothing to commit
IDE users: If your terminal opens in ~ by default, configure it to open in the project root. In VS Code, the integrated terminal automatically opens in the workspace folder.
Multiple projects: If you have nested folders, make sure youโre in the right one:
# โ You're in the parent folder, not the repo
cd ~/projects
git status # fatal: not a git repository
# โ
Go into the actual project
cd ~/projects/my-app
git status # Works
Fix 2: Initialize a new repository
If this is a new project that was never set up with Git:
git init
This creates the .git directory with the default branch. Then add your files:
git add -A
git commit -m "Initial commit"
If you want to connect it to a remote (GitHub, GitLab):
git remote add origin https://github.com/username/repo.git
git push -u origin main
Fix 3: You downloaded a ZIP instead of cloning
When you download a repository as a ZIP from GitHub/GitLab, the .git folder is not included. The files are there, but itโs not a git repository.
# โ Downloaded ZIP โ no .git folder
ls -la
# No .git directory
# โ
Delete and clone properly
rm -rf my-project/
git clone https://github.com/user/repo.git my-project
cd my-project
git status # Works โ full history available
If youโve already made changes to the downloaded files and donโt want to lose them:
# Clone to a temp directory
git clone https://github.com/user/repo.git temp-repo
# Move the .git folder into your working directory
mv temp-repo/.git ./
rm -rf temp-repo
# Now check what changed
git status
Fix 4: The .git folder was accidentally deleted
If someone (or a cleanup script) deleted the .git directory, the repository history is gone locally. But if you have a remote, you can recover:
# If you have a remote, re-clone
git clone https://github.com/user/repo.git temp-clone
mv temp-clone/.git .
rm -rf temp-clone
# Check status โ your local changes will show as modifications
git status
If you donโt have a remote and no backup, the history is permanently lost. You can only start fresh:
git init
git add -A
git commit -m "Re-initialize repository (history lost)"
Prevention: Never run rm -rf .git unless you intentionally want to disconnect from version control. Be careful with cleanup scripts that recursively delete hidden directories.
Fix 5: Git submodule not initialized
If you cloned a repo that contains submodules, the submodule directories exist but are empty (no .git reference inside them).
# You're inside a submodule directory that wasn't initialized
cd libs/shared-utils
git status # fatal: not a git repository
# โ
Go back to the parent repo and initialize submodules
cd ../..
git submodule init
git submodule update
# Or do both in one command
git submodule update --init --recursive
When cloning: Use --recurse-submodules to automatically initialize submodules:
git clone --recurse-submodules https://github.com/user/repo.git
Fix 6: Bare repository confusion
A bare repository (used on servers) has no working directory โ the git data is in the root, not in a .git subfolder.
# โ You cloned with --bare
git clone --bare https://github.com/user/repo.git
cd repo.git
git status # fatal: this operation must be run in a work tree
# โ
Clone normally for development
git clone https://github.com/user/repo.git
cd repo
git status # Works
Fix 7: Filesystem or mount issues
On network drives, Docker volumes, or WSL mounts, the .git folder might not be accessible due to filesystem permissions or case sensitivity.
# Check if .git exists but isn't accessible
ls -la .git
# If "Permission denied" โ fix permissions
chmod -R u+rwX .git
# On WSL with Windows drives
# Git might not recognize the repo due to ownership
git config --global --add safe.directory /mnt/c/Users/you/project
Since Git 2.35.2, Git refuses to operate in repositories owned by a different user (security fix for CVE-2022-24765). If you see โdubious ownership,โ add the directory to the safe list:
git config --global --add safe.directory /path/to/repo
Prevention tips
-
Show git branch in your prompt โ Most shells (zsh, fish, bash with git-prompt) show the current branch. If you donโt see a branch name, youโre not in a repo.
-
Use
git statusas a sanity check before running destructive commands. -
Donโt delete
.gitโ If you want to remove git tracking, userm -rf .gitintentionally, not as part of a cleanup script. -
Clone, donโt download โ Always use
git cloneinstead of downloading ZIPs from GitHub.
FAQ
Can I recover my commit history if .git was deleted?
Only if you have a remote (GitHub, GitLab, etc.) or a local backup. The .git folder IS the repository โ it contains all commits, branches, and history. Without it or a remote copy, the history is gone.
Why do I get this error in my CI/CD pipeline?
Your CI runner might checkout code without the .git directory (shallow clone or archive). Check your CI config โ most systems have an option for full clone vs shallow clone. For GitHub Actions, use fetch-depth: 0 for full history.
I see this error only in VS Codeโs terminal. Why?
VS Code might open the terminal in a different directory than your workspace. Check with pwd. You can configure the terminalโs starting directory in VS Code settings: "terminal.integrated.cwd".
Does git init in an existing repo cause problems?
No. Running git init in a directory that already has a .git folder is safe โ it wonโt overwrite existing history. Git will say โReinitialized existing Git repository.โ