Ffile2fix
Sign in Get started

How to fix "fatal: not a git repository"

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

Git looked for a .git directory in the current folder and every parent folder and found none, so it does not know which repository the command applies to. You are either in the wrong directory, the repository was never initialised or cloned there, or its .git folder was lost.

Also appears as: fatal: not a git repository (or any parent up to mount point /home) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). · fatal: not a git repository: '/home/user/site/.git' · fatal: not a git repository: /var/www/app/../.git/worktrees/feature · fatal: detected dubious ownership in repository at '/var/www/app'

Common causes

  • The terminal is in the wrong directory, often one level above or below the project
  • The project was uploaded by FTP, ZIP or a hosting file manager, which does not include the hidden .git folder
  • git init or git clone was never run for this folder
  • A deploy script or cron job runs git from a different working directory
  • The .git folder was deleted, or a .git file points to a submodule/worktree path that no longer exists
  • GIT_DIR or GIT_WORK_TREE environment variables point somewhere else

How to fix it

  1. Check where you are. Run pwd and ls -la. The project root should contain a .git directory (hidden). If it is in a subfolder, cd into it.
  2. Find the repository. Search for it with find ~ -maxdepth 3 -name .git -type d, then cd into the folder that contains the .git directory you found.
  3. Clone instead of uploading. If the code was uploaded without .git, clone it on the server instead: git clone [email protected]:user/repo.git into an empty folder, or use cPanel's Git Version Control.
  4. Initialise a new repository. If this folder should become a repo, run git init, git add ., git commit -m "Initial commit", then git remote add origin <url>.
  5. Fix scripts and cron jobs. Use git -C /home/user/site pull or cd /home/user/site && git pull so the command runs in the right directory regardless of where the script starts.
  6. Check broken worktree or env vars. Run env | grep GIT and unset stray GIT_DIR values. If .git is a file pointing to a missing path, re-clone or run git worktree prune in the main repository.

Shell

pwd && ls -la              # is there a .git directory here?
git rev-parse --show-toplevel   # works only inside a repo

# run git against a specific folder from anywhere (cron, scripts)
git -C /home/user/site status
git -C /home/user/site pull --ff-only

How to stop it happening again

  • Deploy with git clone/pull or CI instead of uploading files that lack .git
  • Use git -C <path> or absolute paths in scripts and cron jobs
  • Never delete hidden folders when cleaning up project directories

Frequently asked questions

Can I recover the history if .git was deleted?

Only from another copy: clone the remote again into a new folder and copy your working files over it, then review the changes with git status. Without a remote or backup the history is gone.

Why does it mention 'Stopping at filesystem boundary'?

Git stops searching parent directories when it crosses a mount point. Usually you are simply outside the repository; cd into the project folder.

What is 'detected dubious ownership'?

A different error: the repo exists but is owned by another user, and Git refuses to use it for safety. Run git as the owner, or add the path with git config --global --add safe.directory /path if you trust it.