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.

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.
pwd
git rev-parse --show-toplevel # prints the repo root, or the same fatal message
ls -a | headGit 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:
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 yetDiscovery rules that decide the outcome:
| What Git sees | Result |
|---|---|
.git directory with HEAD, objects, refs | repository root |
.git file containing gitdir: /path | worktree or submodule, path must exist |
.git directory that is empty or partial | not a git repository or a corrupt-repo error |
no .git up to / | this fatal message |
GIT_DIR set to a wrong path | this fatal message even inside a repo |
| bare repository as the current directory | commands work, but there is no working tree |
| Situation | Symptom | Confirm with |
|---|---|---|
| Wrong directory | other project files visible | pwd, ls |
| Downloaded archive | no .git, .gitignore present | ls -a |
| Docker build context | repo works locally, not in the image | docker run ... ls -a |
Deleted .git | all history gone locally | ls -a, then re-clone |
| Submodule not initialized | empty directory | git submodule status |
| Worktree link broken | .git file points at a missing path | cat .git |
Key Comparison Table
| Goal | Command | Effect |
|---|---|---|
| Show the repository root | git rev-parse --show-toplevel | absolute path or fatal |
| Run a command elsewhere | git -C /path/to/repo status | no cd needed |
| Start a new repository here | git init | creates .git, keeps files |
| Restore a deleted repository | git clone <url> . | fails on non-empty directory, see below |
| Populate submodules | git submodule update --init --recursive | creates working trees |
| Point at another git dir | git --git-dir=/path/.git --work-tree=/path status | explicit, no discovery |
| List worktrees | git worktree list | shows registered paths |
Recovering a deleted .git in a directory that still has files:
| Case | Command | Result |
|---|---|---|
| Files unmodified, remote exists | git clone <url> /tmp/fresh then copy the files | history restored, edits copied over |
| Same directory required | git init, git remote add origin <url>, git fetch, git reset origin/main | tracked files come from the remote, local edits stay |
| No remote, no backup | git init only | history is not recoverable |
| Stash or index lost | git fsck --lost-found after re-init | only 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
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 # worksWhen 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:
ls -a
# . .. .gitignore README.md src
git status
# fatal: not a git repository (or any of the parent directories): .gitClone 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
cat .git
# gitdir: /home/me/work/main/.git/worktrees/featureWorktrees 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:
git worktree list
git worktree prune
git submodule update --init --recursiveDeleting 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
env | grep ^GIT_
# GIT_DIR=/old/path/.git
# GIT_WORK_TREE=/old/pathGIT_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
WORKDIR /app
COPY . .
RUN git rev-parse --short HEAD # fatal: .git was not copiedA .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
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 treeA 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 documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
Git: The current branch has no upstream branch
The branch exists locally with no tracking configured. Set upstream once with push -u, or let push.autoSetupRemote do it on every new branch.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.