Skip to content
PC Moment
Programming

Fatal: Not a Git Repository: What It Means and How to Fix It

Git says "fatal: not a git repository" when it can't find a .git folder. Here's how to find the right folder, start a repository, and fix GIT_DIR, submodule and ownership problems.

By PC MomentUpdated 7 min read

PC Moment title card for Fatal: Not a Git Repository

You ran git status, git add or git commit and got:

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

Git couldn’t find a repository where you ran the command. Nine times out of ten you’re simply in the wrong folder: cd into your project folder and run the command again. If that doesn’t do it, the causes below cover the rest.

What causes “fatal: not a git repository”

Every Git repository has a .git folder (or a .git file pointing to one) at the top of the project. When you run a Git command, Git checks the current folder for it, then each parent folder in turn. If it reaches the top without finding one, you get this error.

That happens when:

  • You’re in the wrong folder, often the parent folder of your project or a new terminal window that opened in your home folder.
  • The folder was never made into a repository (no git init or git clone).
  • The .git folder was deleted, moved, or never there, for example in a project downloaded as a ZIP.
  • The GIT_DIR environment variable or --git-dir option points to the wrong place.
  • A submodule’s .git file points to a repository folder that no longer exists.

A related message, detected dubious ownership in repository, comes from a security check about who owns the folder. It’s covered at the end.

What you seeLikely causeFix
Error ends with (or any of the parent directories): .gitWrong folder, or the folder isn’t a repositorycd to the project (Fix 1), or git init / git clone (Fix 2)
Project came from a ZIP downloadNo .git folder at allClone the repository (Fix 2)
Error ends with a specific pathGIT_DIR or --git-dir points to the wrong placeClear or correct it (Fix 3)
Path contains .git/modules/Broken submodule .git fileRebuild the submodule (Fix 4)
detected dubious ownership in repositoryFolder owned by another user accountTake ownership or add safe.directory (Fix 5)

How to fix it

Fix 1: Go to the right folder

  1. Check where you are:

    pwd

    In Git Bash, macOS or Linux this prints the current folder. In Windows Command Prompt, use cd with no arguments instead; in PowerShell, pwd works too.

  2. Move into your project folder, the one that contains your code:

    cd ~/projects/my-app

    Replace the path with your own. On Windows in Command Prompt or PowerShell, a path looks like cd C:\Users\you\projects\my-app. If the project is on a different drive in Command Prompt, add /d (cd /d D:\projects\my-app) so the drive changes too.

  3. Confirm Git sees the repository:

    git rev-parse --is-inside-work-tree

    This prints true when you’re inside a repository’s working folder. Outside one, you get the same “not a git repository” error.

Tip: Typing long paths is where typos creep in. Press Tab after the first few letters of a folder name to complete it; our Command Prompt shortcuts guide covers Tab completion and other time savers.

Tip: You don’t have to cd at all. git -C ~/projects/my-app status runs the command as if Git had been started in that folder.

If you’re not sure which folder is the repository, look for the .git folder. On Windows it’s hidden: Git for Windows marks the .git folder as hidden by default (the core.hideDotFiles setting). List it with:

ls -a

in Git Bash, or dir /a in Command Prompt. In File Explorer on Windows 11, select View > Show > Hidden items. On Windows 10, open View > Options > Change folder and search options, then on the View tab select Show hidden files, folders, and drives.

Fix 2: Create or clone the repository

If the folder really isn’t a repository yet, decide which situation you’re in.

Starting a new project from scratch:

git init

This creates an empty repository, a .git folder with its subfolders, in the current folder. To choose the name of the first branch, add -b, for example git init -b main. Running git init again in an existing repository is safe; the docs say it won’t overwrite what’s already there.

Working on a project that already exists on GitHub or another server:

git clone https://github.com/owner/repo.git
cd repo

The first command downloads the full repository, history included, into a new repo folder. The second moves you into it, which is where Git commands work. Forgetting that cd is a common way to hit this error right after cloning.

Note: A project downloaded with GitHub’s Download ZIP button is a snapshot. GitHub’s docs say snapshots don’t contain the repository history and recommend cloning if you want it. Git commands won’t work in the unzipped folder, so clone the repository instead of running git init on top of the ZIP contents.

If you just ran git init on a new project and your first push fails with error: src refspec main does not match any, see how to fix src refspec main does not match any.

Fix 3: Check GIT_DIR and —git-dir

If the error includes a specific path, like this:

fatal: not a git repository: 'C:/old/path/.git'

Git was told exactly where the repository is and didn’t search for it. The GIT_DIR environment variable or the --git-dir option does that: it sets the repository path and turns off the normal folder search.

  1. Check whether GIT_DIR is set. In Git Bash, macOS or Linux:

    echo $GIT_DIR

    In PowerShell, use $env:GIT_DIR. In Command Prompt, use echo %GIT_DIR% (if it prints %GIT_DIR% back, it isn’t set).

  2. If it points to the wrong place, clear it for the current session. In Git Bash:

    unset GIT_DIR

    In PowerShell, use Remove-Item Env:GIT_DIR.

  3. If it comes back every time you open a terminal, remove it from wherever it’s set: your shell profile, a script, or Windows environment variables. Also check any alias or script that passes --git-dir.

Fix 4: Repair a broken submodule

Submodules (and extra worktrees created with git worktree) don’t have a .git folder. They have a small .git file containing a line like gitdir: ../.git/modules/lib that points to the real repository inside the main project. If that folder is gone, for example because the main project was copied without its .git folder, you see:

fatal: not a git repository: C:/projects/app/lib/../.git/modules/lib

To rebuild a broken submodule:

Important: The next step deletes the submodule’s folder. Copy out any changes you made inside it that aren’t pushed anywhere.

  1. Go to the main project’s top folder (the one with .gitmodules).

  2. Delete the broken submodule folder, here lib:

    rm -rf lib
  3. Clone it again from the URL recorded in .gitmodules:

    git submodule update --init

    --init initializes any submodules that haven’t been set up yet and checks them out.

Fix 5: Dubious ownership and safe.directory

Since the security releases that fixed CVE-2022-24765 (Git 2.35.2, plus 2.30.3 through 2.34.2 for older versions), Git refuses to work in a repository owned by a different user account than the one running Git. The message starts like this:

fatal: detected dubious ownership in repository at 'D:/shared/project'

Git then adds a few lines of detail and suggests a git config --global --add safe.directory command for it. This can happen with a repository created by another account or by a tool running as a different user.

Important: This check protects you from running hooks and settings planted by another user. Only add an exception for repositories you trust.

The safer fix is to make your own account the owner of the folder, or to clone a fresh copy under your own user folder. If the shared repository is intentional, trust that one folder:

git config --global --add safe.directory D:/shared/project

This adds the path to your global Git settings. Git only reads safe.directory from protected configuration (system, global or command-line), never from a repository’s own config. Avoid setting it to *: that turns the ownership check off for every repository.

If it still doesn’t work

  • Run git rev-parse --git-dir from the folder. If Git finds a repository, it prints the path to it; if not, it shows the same error, which confirms the problem is the folder.
  • Make sure the command you’re running is a Git command meant for a repository. git clone and git init work anywhere; most others need a repository.
  • If your editor or IDE shows this error, check which folder it opened. Open the project’s top folder, the one containing .git, not a copy of the project or an unrelated parent folder.
  • If the .git folder was deleted and the project was never pushed anywhere, the history is gone. Check your backups or the Recycle Bin before running git init again.

Once the repository is found, these guides help with the next steps: git push set upstream, git undo last commit, and more in the programming section.

Frequently asked questions

What does fatal: not a git repository mean?

Git looked for a .git folder in your current folder and every parent folder and didn't find one. In other words, the folder you're in isn't part of a Git repository.

Should I run git init to fix it?

Only if you want to start a brand-new repository in that folder. If the project already exists on GitHub or another server, go to your existing clone or run git clone instead, so you get its history and remote.

Why can't I see the .git folder on Windows?

Git for Windows marks the .git folder as hidden by default. Turn on hidden items in File Explorer, or run dir /a in Command Prompt or ls -a in Git Bash to list it.

Is running git init in an existing repository dangerous?

No. The git documentation says rerunning git init in an existing repository is safe and won't overwrite what's already there.

What does fatal: not a git repository with a path after it mean?

When the message ends with a path, Git was told exactly where the repository is, through GIT_DIR, --git-dir or a .git file, and that path doesn't hold a repository. Fix or remove whatever points to it.

Related guides