Fix: “this operation must be run in a work tree”
The Error
You run a Git command and see:
fatal: this operation must be run in a work treeThis means Git can find a repository, but the current directory is not a valid working tree. Git commands that interact with files — like git status, git diff, git checkout, and git add— require a working tree. Commands that only deal with refs and objects (like git log and git branch) can work in bare repos.
Common Causes
1. You Are Inside a Bare Repository
A bare repository has no working tree by design. It contains only the Git database (what is normally inside the .git folder). Bare repos are typically used as central remotes on servers.
You can confirm this with:
$ git rev-parse --is-bare-repository
trueFix: If you need a working tree, clone the bare repo into a regular repository, or add a worktree to it:
git clone /path/to/bare-repo.git my-project
cd my-project# From inside the bare repo directory
git worktree add ../my-worktree mainOption B is powerful: many developers use a bare repo as the “hub” and create all their working directories as linked worktrees. This keeps the repo itself clean and lets you have multiple branches checked out simultaneously.
2. You Are Inside the .git Directory
If you accidentally cd into the .git folder (or a subdirectory of it), Git treats you as being in the internal database, not in the working tree.
$ pwd
/home/user/my-project/.git/refs
$ git status
fatal: this operation must be run in a work treeFix: Move back to the project root:
cd /home/user/my-project3. Corrupted or Missing .git File in a Worktree
Linked worktrees (created with git worktree add) do not have a full .git directory. Instead, they have a .git file that points back to the main repository:
gitdir: /home/user/my-project/.git/worktrees/feature-xIf this file is deleted, corrupted, or the path it references no longer exists (e.g., the main repo was moved), Git cannot locate the repository and will fail.
Fix: Recreate the .git file with the correct path:
# Find where the main repo is
# Then create the .git file in your worktree root:
echo "gitdir: /correct/path/to/.git/worktrees/<worktree-name>" > .gitIf the main repo was moved, you also need to update the path stored in the main repo’s worktree config:
# In the main repo's .git/worktrees/<name>/gitdir file,
# update the path to point to the worktree's new location4. GIT_DIR Environment Variable Is Set
Some scripts and tools set the GIT_DIR environment variable to point to a .git directory. If GIT_DIR is set but GIT_WORK_TREE is not, Git knows where the database is but has no working tree.
$ echo $GIT_DIR
/home/user/my-project/.git
$ git status
fatal: this operation must be run in a work treeFix: Unset the variable or also set GIT_WORK_TREE:
# Option A: Unset GIT_DIR
unset GIT_DIR
# Option B: Set both variables
export GIT_DIR=/home/user/my-project/.git
export GIT_WORK_TREE=/home/user/my-project5. Running Git in a Non-Repo Directory
This is less common but can happen if Git finds a .git in a parent directory with core.bare = true set, or if the discovery path is otherwise unusual.
Fix: Make sure you are in the correct directory. Use git rev-parse --show-toplevel to see where Git thinks the working tree root is:
$ git rev-parse --show-toplevel
fatal: this operation must be run in a work tree
# This confirms there is no working tree.
# Navigate to your actual project directory.Diagnostic Checklist
# Am I in a bare repo?
git rev-parse --is-bare-repository
# Where is the .git directory?
git rev-parse --git-dir
# Where is the working tree root?
git rev-parse --show-toplevel
# Is GIT_DIR set?
echo $GIT_DIR
# Is GIT_WORK_TREE set?
echo $GIT_WORK_TREE
# List all worktrees
git worktree list