This error occurs when Git cannot find a valid working tree at the specified path. The fix usually involves navigating to the correct directory, fixing worktree configuration, or cleaning up stale worktree references.
A "working tree" (or work tree) in Git is the directory where your actual project files live—the source code, images, documentation, and everything else that makes up your project. This is separate from the `.git` directory, which contains Git's internal metadata, object database, and configuration. When Git reports that a path "is not a working tree," it means one of several things: you're inside a bare repository (which has no working tree by design), you're inside the `.git` directory itself, the working tree was moved or deleted but Git still has a reference to its old location, or the `core.worktree` configuration points to a non-existent or incorrect path. This error commonly appears when working with `git worktree` commands, when using deployment hooks that reference a separate work tree, or after moving or renaming repository directories. Understanding the difference between a bare repository, a working tree, and the `.git` directory is key to resolving this issue.
First, verify you're not accidentally inside the .git directory:
pwdIf your path ends with /.git or you're inside a bare repository (a directory ending in .git with no working files), navigate up:
cd ..To find your working tree root:
git rev-parse --show-toplevelIf this fails with "fatal: not a git repository," you're not in any Git repository.
Bare repositories don't have working trees—they only contain the .git contents. Check if your repository is bare:
git rev-parse --is-bare-repositoryIf this returns true, you have a bare repository. To work with files, you need to either:
Clone it to create a non-bare repository:
git clone /path/to/bare-repo.git /path/to/working-copy
cd /path/to/working-copyOr use --work-tree for a single command:
git --work-tree=/path/to/checkout --git-dir=/path/to/bare-repo.git checkout HEAD -- .If you've been using git worktree, list all worktrees to find invalid ones:
git worktree listEntries marked as "prunable" point to non-existent directories. Clean them up:
# Dry run first to see what would be removed
git worktree prune --dry-run
# Actually remove stale worktree entries
git worktree pruneThis removes administrative files for worktrees that no longer exist on disk.
Git may be configured to look for a working tree at the wrong location. Check the current setting:
git config core.worktreeIf this shows an incorrect or non-existent path, fix or remove it:
# Set the correct path
git config core.worktree /correct/path/to/worktree
# Or remove the setting entirely
git config --unset core.worktreeAlso check your .git/config file directly:
cat .git/config | grep worktreeLinked worktrees use a .git file (not a directory) that points to the main repository. If a worktree was deleted manually, this file may still exist and cause issues.
Search for these files:
find . -name ".git" -type f 2>/dev/nullIf you find a .git file pointing to a non-existent location, you can safely delete it:
cat ./some-directory/.git
# Shows: gitdir: /path/to/deleted/worktree/.git
rm ./some-directory/.gitEnvironment variables can override Git's default behavior. Check if any are set:
echo $GIT_WORK_TREE
echo $GIT_DIRIf these point to invalid locations, unset them:
unset GIT_WORK_TREE
unset GIT_DIRFor persistent issues, check your shell configuration files (~/.bashrc, ~/.zshrc, etc.) for these variables.
If you need to remove a linked worktree, use the proper Git command instead of deleting the directory manually:
# Remove a clean worktree
git worktree remove /path/to/worktree
# Force remove if there are uncommitted changes
git worktree remove --force /path/to/worktree
# Remove a locked worktree
git worktree remove --force --force /path/to/worktreeAfter removal, the worktree entry is automatically cleaned from Git's administrative files.
After applying the relevant fixes, verify Git is working correctly:
# Check you're in a valid working tree
git rev-parse --show-toplevel
# Verify worktree list is clean
git worktree list
# Test a basic command
git statusAll commands should now work without the "not a working tree" error.
### Bare Repositories and Deployment
Bare repositories are commonly used as central remote repositories (like those on GitHub or GitLab) and in deployment workflows. They contain only the Git metadata without any working files, which is why commands like git status don't make sense in them.
For deployment with a bare repository and separate work tree:
# In a post-receive hook
GIT_WORK_TREE=/var/www/mysite git checkout -f### Worktree vs Work Tree
Note that git worktree (the command for managing multiple working trees) and "work tree" (the directory with your checked-out files) are related but distinct concepts:
- Work tree: The single directory where files are checked out
- git worktree: A feature allowing multiple work trees from one repository
### Automatic Cleanup
Git automatically cleans up stale worktree entries during garbage collection. The timing is controlled by:
git config gc.worktreePruneExpireDefault is "3 months ago." You can run cleanup manually anytime with git worktree prune.
### Locking Worktrees
If a worktree is on a removable drive or network share that's not always mounted, you can prevent it from being pruned:
git worktree lock /path/to/worktree --reason "On removable drive"
git worktree unlock /path/to/worktree # When donewarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server