Git: "fatal: Not a Git Repository (or Any of the Parent Directories)"

Git walked up the directory tree and found no .git. Wrong directory, a missing/bare .git, or HOME/ENV pointing elsewhere — three checks identify which.

What you'll see

Root causes

Not in the repo tree (or moved out of it)

The cwd is outside any directory containing .git. git rev-parse --show-toplevel (in the repo) vs pwd confirms. Subshells and scripts often cd elsewhere first — the classic silent killer.

Broken .git directory (or it's a file with a bad gitdir)

Interrupted clones, partial restores, or a .git that's a gitdir-pointer file (worktrees/submodules) whose target moved. ls -la .git — a valid repo has HEAD, objects/, refs/; a pointer file must resolve.

GIT_DIR/GIT_WORK_TREE env or wrong HOME in CI

Exported GIT_DIR from a previous command points git at a nonexistent repo. env | grep GIT finds it in seconds — cron/CI contexts often have stale env or different HOME.

Fix it

  1. Check where you actually are
    pwd ; ls -a | head -5 ; git -C /path/to/repo status   # -C targets the repo explicitly
  2. Inspect the .git directory's health
    ls .git/HEAD .git/objects .git/refs 2>&1 ; cat .git 2>/dev/null   # pointer file for worktrees
  3. Check for env overrides
    env | grep -i '^GIT' ; unset GIT_DIR GIT_WORK_TREE
  4. Fix the tree: re-init or re-clone deliberately
    git init   # in an existing repo dir with damaged .git: repairs structure (commits intact if objects survive)

Field note

git -C <path> sidesteps every 'wrong directory' failure in scripts: it errors explicitly instead of guessing from cwd. Re-init (git init) over a damaged repo is non-destructive: it rebuilds the metadata; whether data survived depends on .git/objects.

Common questions

It worked yesterday in the same directory. What changed?

Check the .git dir still exists and you didn't move the whole tree (mv breaks relative worktree pointers). ls -a is faster than any theory — if .git is gone or gutted, restore from your remote/backup.

Why does CI fail with this while my shell works?

CI clones into a different path (or hasn't cloned yet) and may carry GIT_* env vars. Print pwd + env | grep GIT in the pipeline and compare to your local reality.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.