Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git: fatal: not a git repository (or any parent): .git

The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.

~5 min readGit
#git#repositories#troubleshooting#worktree
Bold white headline reading NOT A REPO with a Git branch crate standing alone while an amber missing .git slot stays empty.

Introduction & Quick Summary

A script that worked in one shell fails in another:

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

Quick answer: the command ran in a directory that is not inside a working tree. Either the path is wrong, or the .git directory (or file) is missing from what looks like the project root.

bash
pwd git rev-parse --show-toplevel # prints the repo root, or the same fatal message ls -a | head

Git searches the current directory and every parent for a .git entry, stopping at the filesystem root. A directory that contains project files but no .git is just a folder. That is why this error most often follows a cd into a sibling directory, a downloaded ZIP, a Docker COPY that excluded hidden files, or a cleanup command that removed the repository metadata along with build output.

The message names (or any of the parent directories): .git, which is the part to read: Git did check parents. So the answer is either "you are outside the repository" or "the repository metadata is gone".

Core Mechanics, Signatures & Reproducible Setup

Reproduce in a temp directory:

bash
mkdir -p /tmp/nested/project/src && cd /tmp/nested/project/src git status # fatal: not a git repository (or any of the parent directories): .git cd /tmp/nested/project && git init cd src && git status # On branch master / No commits yet

Discovery rules that decide the outcome:

What Git seesResult
.git directory with HEAD, objects, refsrepository root
.git file containing gitdir: /pathworktree or submodule, path must exist
.git directory that is empty or partialnot a git repository or a corrupt-repo error
no .git up to /this fatal message
GIT_DIR set to a wrong paththis fatal message even inside a repo
bare repository as the current directorycommands work, but there is no working tree
SituationSymptomConfirm with
Wrong directoryother project files visiblepwd, ls
Downloaded archiveno .git, .gitignore presentls -a
Docker build contextrepo works locally, not in the imagedocker run ... ls -a
Deleted .gitall history gone locallyls -a, then re-clone
Submodule not initializedempty directorygit submodule status
Worktree link broken.git file points at a missing pathcat .git

Key Comparison Table

GoalCommandEffect
Show the repository rootgit rev-parse --show-toplevelabsolute path or fatal
Run a command elsewheregit -C /path/to/repo statusno cd needed
Start a new repository heregit initcreates .git, keeps files
Restore a deleted repositorygit clone <url> .fails on non-empty directory, see below
Populate submodulesgit submodule update --init --recursivecreates working trees
Point at another git dirgit --git-dir=/path/.git --work-tree=/path statusexplicit, no discovery
List worktreesgit worktree listshows registered paths

Recovering a deleted .git in a directory that still has files:

CaseCommandResult
Files unmodified, remote existsgit clone <url> /tmp/fresh then copy the fileshistory restored, edits copied over
Same directory requiredgit init, git remote add origin <url>, git fetch, git reset origin/maintracked files come from the remote, local edits stay
No remote, no backupgit init onlyhistory is not recoverable
Stash or index lostgit fsck --lost-found after re-initonly objects that were still on disk

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. The directory is right, the parent is the problem

bash
cd ~/work/api # looks like the project git status # fatal ls -a # .env, src, package.json, no .git cd ~/work # the repository root is one level up git status # works

When the project lives inside a larger monorepo, the repository root may be several levels up and the inner folder may have its own ignore rules. git rev-parse --show-toplevel prints the true root, and git -C runs a command there without changing the shell's directory.

2. Downloads, archives and copied folders

A ZIP from a hosting UI contains the working tree but never the .git directory. The result looks complete and fails at the first git command:

bash
ls -a # . .. .gitignore README.md src git status # fatal: not a git repository (or any of the parent directories): .git

Clone instead of downloading when history matters. When only the files are needed, the archive is fine — just do not expect git commands to work in it. Copying a folder between machines with scp or a file manager can drop hidden entries for the same reason.

3. .git as a file: worktrees and submodules

bash
cat .git # gitdir: /home/me/work/main/.git/worktrees/feature

Worktrees and submodules store a pointer file instead of a directory. If the target is missing — the main checkout was moved, or a worktree was deleted with rm -rf instead of git worktree remove — every command fails. Repair paths:

bash
git worktree list git worktree prune git submodule update --init --recursive

Deleting a worktree directory by hand leaves a registration behind, and a stale registration can make a new git worktree add fail with a path that "already exists". prune clears those entries.

4. Environment variables that point somewhere else

bash
env | grep ^GIT_ # GIT_DIR=/old/path/.git # GIT_WORK_TREE=/old/path

GIT_DIR overrides discovery entirely. A value left over from a script, a CI step or a direnv file produces this fatal message inside a perfectly valid repository. Unset them in the current shell (unset GIT_DIR GIT_WORK_TREE) and check shell profiles and CI environment blocks. Environment files are easier to compare across machines with the env formatter.

5. Containers and CI working directories

dockerfile
WORKDIR /app COPY . . RUN git rev-parse --short HEAD # fatal: .git was not copied

A .dockerignore that excludes .git, or an image built from a tarball context, has no repository metadata. Options, in order of preference: bake the commit hash as a build argument (--build-arg GIT_SHA=$(git rev-parse --short HEAD)), copy .git deliberately for build-time tooling, or drop the git call. In CI, a step that runs before actions/checkout or in a different working directory hits the same message.

6. Nested repositories and bare clones

bash
git init inner # creates inner/.git, unrelated to the outer repo git clone --bare <url> repo.git cd repo.git && git status # fatal: this operation must be run in a work tree

A nested repository is invisible to its parent: the outer repo treats inner/ as an untracked directory or a gitlink, and commands inside it use the inner history. A bare repository is the opposite case — the current directory is the git dir and there is no working tree, so status fails with a different message. Use git --git-dir=repo.git --work-tree=checkout when a temporary worktree is genuinely needed, or git clone repo.git checkout instead. Deleting a stray nested .git is safe only after confirming nothing unique lives there; compare the two trees first with the text diff tool.

Common Compilation/Runtime Mistakes & Version Gotchas

Running git init to make the error go away. It creates a new empty repository inside whatever directory you are in, which can bury the real project one level up and produce a nested repo. Confirm the intended root first.

Deleting .git to "reset" a checkout. It removes every commit, branch, stash and reflog entry that existed only locally. If the goal is to discard changes, use git checkout -- . or a fresh clone into another directory.

Cloning into a non-empty directory. git clone <url> . fails when files exist. Clone to a temporary path and copy the files over, or git init plus git fetch plus git reset.

Assuming the shell's directory. A script that cds into a subdirectory and then runs git fails silently in automation. Use git -C with an absolute path.

Confusing a bare repository with a broken one. The error text differs; treating a bare clone as corrupt wastes time. Check for HEAD, objects, refs and the absence of a working tree.

Version notes. Git 2.5 introduced worktrees and the .git file form; anything older cannot open one. Git 2.34 changed git init to use main as the default branch name under some configurations, which matters when a recovery script assumes master. Git 2.45 added git refs subcommands for inspecting reference storage, useful when a .git directory is present but unreadable.

Reference material: git-rev-parse and git-worktree.

Official references

primary documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.