Setup & configAlways fixable

Fix 'fatal: not a git repository'

Git says 'not a git repository (or any of the parent directories): .git' — what does that mean?

If you’re seeing this error

fatal: not a git repository (or any of the parent directories): .git
fatal: not a git repository: '<path>'

You’re in the right place — the fix is below.

Short answer

You are standing somewhere Git does not consider a repository. Run pwd and ls -a: if there is no .git folder here or in any parent, either cd into the project, or run git init to create one. If .git used to be here and has vanished, the repository is not broken so much as missing — check whether you deleted it or are in a copied folder.

quick fix
pwd
ls -a          # is there a .git directory?
cd path/to/your/project

Almost always a wrong-directory problem, not a broken repository

Does this match your situation?

  • 'fatal: not a git repository (or any of the parent directories): .git'
  • Every Git command fails the same way, including git status.
  • It worked yesterday in the same terminal.
  • You just downloaded a project as a ZIP rather than cloning it.

Step-by-step fix

  1. Check where you actually are

    Git walks up from the current directory looking for .git and gives up at the filesystem root. The overwhelmingly common cause is being one level above or below the project — a terminal tab left in your home directory, or a folder that contains the project rather than being it.

    step 1
    pwd
    ls -a
    ls */.git 2>/dev/null      # is the repository one level down?
    
    Show the Windows / PowerShell version
    Windows / PowerShell
    Get-Location
    Get-ChildItem -Force
    Get-ChildItem -Directory | Where-Object { Test-Path "$($_.FullName)\.git" }
    
  2. Ask Git where it thinks the repository is

    rev-parse is the definitive answer. If it prints a path, you are inside a repository and the error came from something else — a different terminal, a script that changed directory, or a tool running elsewhere.

    step 2
    git rev-parse --show-toplevel
    git rev-parse --git-dir
    
  3. If it never was a repository, initialise one

    A folder downloaded as a ZIP from GitHub has no history at all — the .git directory is not included in the archive. Initialising creates a fresh repository with no connection to the original, which is fine for your own project but means you have no upstream history.

    step 3
    git init
    git add .
    git commit -m "Initial commit"
    git remote add origin <url>
    

    If you wanted the project's history, delete the folder and git clone <url> instead. A ZIP download can never be turned back into a repository with history.

  4. If .git disappeared, look before you re-init

    Running git init in a folder whose .git was deleted does not restore anything — it starts an empty history and makes recovery harder by making everything look normal. Check the recycle bin, your backups and any sibling copy of the folder first.

    step 4
    ls -a ..                  # a .git in the parent?
    find . -maxdepth 3 -name .git -type d
    
  5. Check for the environment variables that override everything

    GIT_DIR and GIT_WORK_TREE force Git to look somewhere specific and produce this exact error when they point at a stale path. They are usually left behind by a script or a dotfile.

    step 5
    echo "$GIT_DIR $GIT_WORK_TREE"
    unset GIT_DIR GIT_WORK_TREE
    

Why this works

Git has no daemon and no registry of your projects. Every command starts by looking for a .git directory in the working directory, then in its parent, and so on up to the root of the filesystem — and if it reaches the top without finding one, it has nowhere to read objects or refs from and stops. That is the whole of this error. It says nothing about the repository being damaged, because from Git's point of view there is no repository here at all; the .git directory is the repository, and everything else in the folder is just files.

If that didn’t work

  • In a submodule, .git is a file containing 'gitdir: ...' rather than a directory — check the path it names still exists.
  • On a network drive or inside a container, confirm the mount is actually present before blaming Git.
  • If .git exists but Git still refuses, see the dubious-ownership guide — that error is sometimes reported similarly by IDEs.
  • A .git folder that exists but is empty means an interrupted clone: delete it and clone again.

How to stop it happening again

  • Clone repositories rather than downloading ZIPs when you want history.
  • Add the current branch to your shell prompt — the absence of one tells you immediately you are outside a repository.
  • Use git rev-parse --show-toplevel at the top of scripts rather than assuming a relative path.
  • Never delete .git to 'clean up' a folder; it is the entire project history.

Commands used in this guide

git init

Turn the current folder into a Git repository.

git clone

Copy a remote repository, its full history and its branches, onto your machine.

git rev-parse

Turn any revision expression into a concrete commit hash.

git status

Show what's changed, what's staged, and what Git is ignoring.

git config

Read and write Git settings for one repo, your user, or the whole machine.

Still stuck?

Search the full command reference and every other rescue guide — there are 76 of them, covering everything from detached HEAD to force-push disasters.

Browse all guides →

Frequently asked questions

What does 'fatal: not a git repository (or any of the parent directories)' mean?

Git searched the current folder and every folder above it for a .git directory and found none, so there is no repository here to operate on. Nearly always you are in the wrong directory — cd into the project. If the folder genuinely is not a repository yet, git init makes it one.

Does git init fix this error?

It stops the error, but only by creating a brand-new empty repository. If the folder previously had history, git init does not bring it back and makes the loss harder to spot. Only run it when you know the folder was never a repository.

Why do I get this after downloading a project as a ZIP?

GitHub's ZIP archive contains the files at one commit and nothing else — the .git directory is deliberately excluded, so there is no history, no branches and no remote. Clone the repository instead if you need any of that.

How do I find the root of my repository from a subfolder?

git rev-parse --show-toplevel prints the absolute path of the top of the working tree, and git rev-parse --git-dir prints where .git actually is. Both fail with this same error when you are outside a repository, which makes them a quick way to test.

Can I get my history back if I deleted the .git folder?

Not from Git — that folder was the entire repository. Your options are your operating system's recycle bin or file-versioning feature, a backup, another clone on a colleague's machine, or the copy on your remote host. Clone the remote again rather than running git init over the top.

Related rescue guides

Always fixable

Git says 'detected dubious ownership in repository' — how do I fix it?

Git 2.35.2 added a security check that refuses to run in a repository owned by a different user. Add the path to safe.directory and the error goes away: git config --global --add safe.directory /path/to/repo. On your own single-user machine that is entirely reasonable; on a shared box, work out why the ownership is wrong before you silence the warning.

Read the fix →
Usually recoverable

Git says 'object file is empty' or 'loose object is corrupt' — can I repair it?

Corruption is almost always a few damaged object files after a crash, disk problem or a killed process. Run git fsck to identify them, delete the empty or unreadable ones, and re-fetch the good copies from your remote. If you have a remote, a fresh clone is often the fastest fix.

Read the fix →
Always fixable

Git says another git process seems to be running — how do I fix index.lock?

Git locks the index while it works and removes the lock when it finishes. A crashed command, a killed process or an editor's background Git integration can leave the lock behind. Confirm nothing is actually running, then delete .git/index.lock.

Read the fix →
Always fixable

How do I change the remote URL of a Git repository?

git remote set-url origin <new-url> repoints an existing remote. It's a purely local change — nothing on either server is touched — so it's the safe way to switch between HTTPS and SSH, move to a new host, or fix a URL after renaming the repository.

Read the fix →
← All Git Rescue guides