Fix “fatal: not a git repository” Error in VS Code



Disclosure: This post contains affiliate links. If you purchase through our links, we may earn a commission at no extra cost to you. We only recommend tools we’ve researched and believe are genuinely useful.

You open the integrated terminal in VS Code, type a git command out of habit, and get fatal: not a git repository (or any of the parent directories): .git. Meanwhile the Source Control panel in the sidebar looks fine, or maybe it’s showing nothing at all, which is its own clue. Almost always this comes down to VS Code opening the wrong folder, a missing or corrupted .git directory, or a workspace multi-root setup pointing at a path that isn’t actually a repo. None of those take long to sort out — figure two minutes if it’s a path issue, five if you’re rebuilding a broken .git folder. Below is the exact order to check things, starting with the fix that’s wrong about 70% of the time.

Key Takeaways

  • Initialize a Git repository in your project folder using git init to create the required .git directory structure.
  • Ensure your VS Code workspace root matches your Git repository location to avoid path mismatch errors.
  • Check that the .git folder exists and is not hidden; unhide it on Windows via folder properties.
  • Clone repositories properly using git clone to automatically set up Git tracking and avoid initialization issues.
  • Verify Git initialization success by running git status to confirm your project is tracked correctly.

Quick Fix

Run git init in your project folder to initialize a Git repository and create the .git directory. If you cloned a repository, verify the .git folder exists by checking hidden files on Windows. Confirm success with git status.

git status

What “fatal: not a git repository” means and when you’ll see it

Git throws this error whenever a command that needs repository context runs from a directory that isn’t a repo and has no parent directory that’s a repo either. The exact wording is fatal: not a git repository (or any of the parent directories): .git — git walks up the directory tree looking for a .git folder, hits your filesystem root or a mount boundary, and gives up. It’s not telling you anything is broken. It’s telling you it’s standing in the wrong place.

Inside VS Code, this shows up in a few predictable spots. The integrated terminal (VS Code 1.95 and later, on Windows, macOS, and Linux) can silently drop you into a subfolder or a completely different workspace root than you expect, especially after a “Reopen Folder” action or a devcontainer rebuild. External terminals — a separate PowerShell or iTerm2 window you cd‘d into manually — hit the same wall if you never actually cd‘d into the repo. And Git extensions like GitLens or the built-in Source Control panel will sometimes report a clean, empty state instead of erroring loudly, which is easy to misread as “nothing’s wrong here.”

This is a path and initialization problem, not a corruption problem. Nothing about your commit history, branches, or objects is damaged just because you got this message. Save the panic for later — first confirm where VS Code actually thinks you are, and where your .git folder actually lives. Those two things not matching is the entire bug about 70% of the time.

You’re running Git commands in the wrong folder—initialize the repo first

Before you touch anything, confirm you’re actually looking at a fresh project that’s never been version-controlled. Open the integrated terminal in VS Code with Ctrl+` (Windows/Linux) or Cmd+` (macOS), then check where you are.

pwd

On macOS/Linux, this prints your current working directory — confirm it matches the folder path shown in VS Code’s title bar or Explorer sidebar. On Windows (PowerShell or cmd), use cd with no arguments to print the same thing.

If this is genuinely a new project with no .git folder anywhere in its parent tree, you just haven’t initialized it yet. This is the cause about 60% of the time this error shows up in a freshly opened VS Code folder.

Initialize a new Git repository in your project folder

git init

This creates a hidden .git folder in your current directory and starts tracking it as a repository. Expect output like:

Initialized empty Git repository in /Users/you/projects/myapp/.git/

Confirm it worked with:

git status

You should see On branch main (or master, depending on your Git config defaults) and either “No commits yet” or a list of untracked files. Either output means the repo is live — the fatal error is gone.

To physically verify the .git folder exists, list hidden files:

ls -la

On Windows, use dir /a in cmd or Get-ChildItem -Force in PowerShell. VS Code hides .git from the Explorer sidebar by default (it’s in .vscode/settings.json under files.exclude), so seeing nothing there doesn’t mean nothing happened — the terminal listing is the source of truth.

git init is non-destructive. It never touches existing files, never deletes anything, and running it twice in the same folder is harmless — Git just tells you the repo already exists and moves on. This fix applies the same way on Git 2.40 and later, across Windows, macOS, and Linux, and inside VS Code 1.95+.

Your VS Code workspace folder doesn’t match your Git repository location

If git init feels wrong — because you know a repo already exists somewhere on disk — the real problem is probably that VS Code opened the wrong folder. This happens constantly with cloned repos that unzip into an extra nested directory, or projects where you clicked “Open Folder” one level too high or too low. VS Code doesn’t walk up or down the tree looking for .git; it only checks the exact folder you gave it.

Locate your .git folder and open that directory in VS Code

First, find out if you’re anywhere inside a valid repo, even a subfolder:

git rev-parse --show-toplevel

Run this from a terminal in the suspect folder. If it prints a path like /Users/you/projects/myapp, that’s your real repo root — copy it. If it prints fatal: not a git repository (or any of the parent directories): .git, there’s no repo above or below where you’re standing, and you need to search wider.

find . -name .git -type d

On macOS/Linux, this searches recursively from your current directory for any .git folder. On Windows, use:

dir /s /b .git

Both list full paths to every .git directory found. Strip the trailing /.git from whatever path comes back — that parent folder is what you open in VS Code.

In VS Code 1.95+, go to File → Open Folder (not “Add Folder to Workspace” unless you mean to), and pick that exact directory. Once it’s open, check the status bar at the bottom-left — it should now show a branch name with a branch icon instead of no Git indicator at all. The breadcrumb path at the top of the editor should also reflect the repo root.

One trap: multi-root workspaces (.code-workspace files) can have one folder correctly pointing at a repo root and a second folder pointing at some unrelated directory. VS Code’s Source Control panel will only show Git status for folders that actually contain .git — if a second root shows no Git pane at all, that’s the mismatched one. Applies to Git 2.13+ and all current VS Code builds.

You cloned a repository but Git isn’t recognizing it as a repo

Sometimes the folder exists, VS Code opened it fine, but the Source Control panel is empty and the terminal says:

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

This usually means one of three things: the clone got interrupted mid-transfer, the .git folder got corrupted, or someone (possibly you, possibly a cleanup script) deleted it. Check first:

ls -la

On macOS/Linux, this lists hidden files including .git. If it’s missing entirely, that’s your answer. On Windows, hidden folders don’t show in Explorer by default — enable View → Show → Hidden items in Explorer, or run dir /a in PowerShell/CMD to check.

Re-clone the repository if the clone was incomplete

If this repo was cloned recently and you have no uncommitted local changes worth keeping, the fastest fix is to delete the broken folder and pull a fresh copy.

Warning: the command below permanently deletes the entire folder and everything in it, including any uncommitted work. Confirm you have nothing unsaved before running it.

rm -rf myapp

On Windows CMD, use rmdir /s /q myapp instead — same effect, no confirmation prompt, no undo.

git clone https://github.com/you/myapp.git

Expect output like:

Cloning into 'myapp'...
remote: Enumerating objects: 1284, done.
Receiving objects: 100% (1284/1284), 2.1 MiB | 4.2 MiB/s, done.

Once it finishes, run git config --list inside the new folder to confirm remote.origin.url and user.name/user.email are set correctly, then reopen the folder in VS Code.

The .git folder is hidden or corrupted on Windows

Windows 10 and 11 treat .git as a hidden system-style folder more aggressively than macOS or Linux does, and some antivirus tools flag the folder’s loose object files as suspicious and quarantine them. If VS Code’s Source Control tab shows nothing and the integrated terminal throws fatal: not a git repository (or any of the parent directories): .git on a project you know was cloned correctly, this is often the reason. This applies to Git 2.40 and later on Windows 10/11 — behavior around hidden attributes hasn’t changed in earlier or later builds, so don’t bother checking your Git version first.

Unhide the .git folder on Windows and check antivirus logs

Open a terminal in the project root and check whether the folder is actually there but flagged hidden:

dir /a

The /a flag lists all files regardless of hidden or system attributes. If .git shows up here but not in a plain dir or in File Explorer, the hidden flag is the problem, not a missing folder.

attrib -h .git

This clears the hidden attribute on the folder. No output means it worked — run dir again to confirm .git now appears normally.

If the folder is genuinely gone or partially empty, check Windows Defender before assuming it’s deleted for good. Go to Settings → Privacy & security → Windows Security → Virus & threat protection → Protection history and look for entries referencing your project path. Defender occasionally quarantines files inside .git\objects during a clone from an untrusted network share. If you find a match, restore it, then add the project folder to Manage settings → Exclusions to stop it from happening again.

Once the folder is unhidden or restored, confirm with:

git status

You should see the usual branch and staged-changes output instead of the fatal error. Reload the VS Code window (Ctrl+Shift+P → “Reload Window”) afterward — the Source Control panel caches its state and won’t pick up the fix until then.

Verify the fix worked and confirm Git is tracking your project

Don’t trust a silent fix. Run through these checks in order before you go back to committing code — each one confirms a different layer of the repository is actually intact, not just that the .git folder exists.

Run these commands to confirm your repository is live

git status

You should see something like On branch main followed by either nothing to commit, working tree clean or a list of modified files. If you still get fatal: not a git repository, the earlier fix didn’t stick — go back and confirm you ran it from the correct folder.

git log --oneline -3

This lists your three most recent commits as short hashes with messages, for example a1b2c3d Fix login redirect. If this returns fatal: your current branch 'main' does not have any commits yet, Git is initialized but the history is missing — you’re looking at a different problem, likely a bad clone or a reset gone wrong.

git remote -v

This should print your upstream URLs for fetch and push, like origin https://github.com/you/project.git (fetch). Empty output means no remote is configured, which is fine for local-only repos but worth noting if you expected one.

Last check: look at the Source Control icon in VS Code’s left sidebar (the branching icon, third from top by default on Windows, macOS, and Linux). It should now show your branch name in the status bar bottom-left, plus any changed file count as a badge — not a warning triangle or blank panel.

Prevent this error in the future: best practices for Git in VS Code

Most repeats of this error come down to one habit: opening the wrong folder. Always run git init or git clone into the exact directory you plan to open with File > Open Folder — not its parent, not a subfolder inside it.

Before you write a single line of code, glance at the Source Control panel (VS Code 1.95+ shows a branch name and sync arrows directly in the status bar). If it’s blank or shows a warning triangle, deal with it before you start editing — not after you’ve written 200 lines you can’t easily diff.

Turn on auto-fetch so sync problems surface early instead of during a panicked push:

"git.autofetch": true

Add this to your settings.json, or toggle Git: Autofetch in the Settings UI under Extensions > Git. VS Code will fetch from your remote periodically and flag divergence before it turns into a merge fight.

Two more habits worth keeping: add a .gitignore on day one (use gitignore.io or your language’s template) so you’re not tracking node_modules or build output, and make your first commit immediately after git init. An empty repo with no commits hides problems that an early git status would have caught.

Frequently Asked Questions

Why does VS Code say fatal: not a git repository?

This error occurs when you run Git commands in a folder that hasn’t been initialized as a Git repository. VS Code cannot find the .git directory needed to track your project. Initialize the repository with git init to resolve it.

How do I initialize a Git repository in VS Code?

Open the terminal in VS Code, navigate to your project folder, and run git init. This creates a .git directory that marks the folder as a Git repository. Afterward, run git status to confirm initialization succeeded.

Is the .git folder hidden on Windows and how do I see it?

Yes, the .git folder is hidden by default on Windows. Show hidden files by opening File Explorer, clicking View, and checking Show hidden files. Alternatively, enable hidden files in VS Code’s settings to display the .git folder.

Can cloning a repository fail to set up Git tracking?

Cloning typically sets up Git tracking automatically, but the error may occur if the clone was incomplete or the .git folder was deleted. Re-clone the repository using git clone with the correct repository URL to restore full Git functionality.

Scroll to Top