Sub topic 12
Limitations and caveats
Worktrees remove the need to stash or re-clone, but they have limits. This chapter covers where the model stops working, which errors you will meet, and when another tool is the better choice. It assumes you have read the earlier chapters: you can create, list and remove worktrees, and you know that a branch can be checked out in only one worktree at a time.
A short map of the limits
The limits come from one design fact. A linked worktree has its own working files, index and HEAD. Everything else lives in the shared repository. Most surprises follow from one of two things:
- Something you expected to be shared is per worktree, such as untracked files.
- Something you expected to be separate is shared, such as the stash.
The one-branch rule in practice
Git refuses to check out a branch that another worktree already has. The exact wording depends on your Git version.
git switch feature/search
# fatal: 'feature/search' is already checked out at '/home/dev/myproject-feature'Find where the branch is checked out, then pick one of these:
git worktree list- Work in the worktree that already has the branch. This is usually right.
- Remove or switch that worktree if you no longer need it.
- Create a new branch from the same commit, with
git switch -c feature/search-2 feature/search. - Use
git worktree add --detachif you only need to read or test the code.
The rule applies to more than switch. git branch -d and git branch -D refuse to delete a branch that any worktree has checked out. A branch in the middle of a rebase also counts as checked out in the worktree running the rebase.
git worktree add --force overrides the check. Avoid it. Two worktrees on one branch can each move the branch ref, and then one worktree's index and files no longer match what the branch points to. You then have to untangle that mismatch by hand.
Shared state that surprises people
The shared repository holds branches, tags, remote-tracking refs, objects, the stash, configuration and hooks. Each of these shares in a way that can bite.
The stash is shared
refs/stash lives in the common directory. A stash made in one worktree shows up in git stash list in every other worktree. Running git stash pop in the wrong one applies changes to the wrong branch. Name your stashes so they are easy to tell apart:
git stash push -m "hotfix-wip: login crash experiment"
git stash listIf you adopt worktrees to avoid stashing, the stash stops being a problem.
Configuration and hooks are shared
Repository config (.git/config) and hooks apply to every worktree. A setting such as user.email applies everywhere. You cannot give one worktree its own core.sparseCheckout without extra setup. For per-worktree settings, enable the extension and write to the worktree config file:
git config extensions.worktreeConfig true
git config --worktree core.sparseCheckout trueTest this on a throwaway repository first. Once extensions.worktreeConfig is on, some settings such as core.bare and core.worktree need care, and older Git versions that don't know the extension will refuse to work with the repository.
Hooks run with the current worktree as the working directory, but the hook scripts themselves are the shared ones. A hook that assumes the repository root is the folder containing .git can fail in a linked worktree, where .git is a file and not a directory. Use git rev-parse --show-toplevel and git rev-parse --git-common-dir in hook scripts. Don't build paths by hand.
What is per worktree
These are separate in each worktree:
HEAD, the index, and the files on disk- In-progress state for merge, rebase, cherry-pick and bisect
- The
HEADreflog
Running git bisect in two worktrees at once works. Each has its own bisect state. Be careful when you try to finish a merge or rebase from a different directory than the one that started it.
Untracked, ignored and dependency files
A new worktree contains only tracked files. It does not contain:
.envfiles and local credentialsnode_modules, virtual environments and build output- Editor settings that are git-ignored
- Local databases or caches
This is the most common cause of "it works in my main directory but not in the new one." Plan a setup step for each new worktree. A small script works well:
#!/usr/bin/env bash
set -euo pipefail
src=$(git rev-parse --show-toplevel)
dest="$1"
git worktree add "$dest" "${@:2}"
if [ -f "$src/.env" ]; then
cp "$src/.env" "$dest/.env"
fi
( cd "$dest" && npm ci )Call it as ./add-wt.sh ../myproject-hotfix -b hotfix/login-crash main. Run it from the worktree whose .env you want copied.
Two cautions apply to dependencies.
- Don't symlink dependency directories between worktrees. If two branches need different dependency versions, one of them silently runs against the wrong one. A symlink is only safe when the lock files are identical and nothing writes into the directory.
- Expect disk use to grow. Git objects are shared, so history costs nothing extra. Checked-out files and dependencies are copied per worktree. Ten worktrees with 500 MB of
node_moduleseach is 5 GB.
Also watch for fixed resources. Two worktrees that start a dev server on port 3000, or write to the same local database file outside the repository, will collide. Build tools with caches keyed to an absolute path will rebuild from scratch in each worktree.
Removing and losing work
Removal refuses unsafe cases
git worktree remove refuses a worktree with modified or untracked files. It also refuses a locked worktree, and one that contains submodules. Use --force to override the first and last. A locked worktree needs --force twice. Check first, because forced removal deletes uncommitted work permanently.
A quick check across all worktrees before cleanup:
git worktree list --porcelain \
| awk '/^worktree /{print substr($0, 10)}' \
| while read -r wt; do
echo "== $wt"
git -C "$wt" status --short
doneDetached HEAD commits can disappear
Commits made on a detached HEAD belong to no branch. The only reference to them is the HEAD reflog, and that reflog is per worktree. Removing the worktree deletes it. After that, the commits are unreachable and git gc can eventually delete them. Before removing a detached worktree that has commits you want, give them a branch:
git -C ../myproject-review switch -c review/search-fixes
git worktree remove ../myproject-reviewDeleting the directory by hand
If you delete a worktree folder with rm -rf or a file manager, the metadata stays in .git/worktrees/. The branch stays "checked out" there, so the one-branch rule still blocks you. Run git worktree prune, or let git gc do it. git gc only prunes entries older than gc.worktreePruneExpire, which is three months by default.
This also affects worktrees on removable drives and network shares. If the drive is unplugged during a prune, Git sees a missing directory and may discard the entry. Lock those worktrees:
git worktree lock --reason "on removable drive" ../myproject-reviewSubmodules
The Git documentation says multiple checkouts are still experimental and that submodule support is incomplete. It recommends against making several checkouts of a superproject. In practice:
- A new worktree does not initialize submodules. Run
git submodule update --initinside it. - Each worktree gets its own copy of submodule working files, and the submodule's repository data goes under the superproject's worktree-specific directory.
git worktree removerefuses to delete a worktree that contains submodules unless you force it.
If your project depends heavily on submodules, a second clone is the safer option.
Paths, versions and platforms
- Absolute paths. By default Git records absolute paths in both directions: the repository points to each worktree, and each worktree's
.gitfile points back. If you move the main repository or a worktree by hand, the links break. Usegit worktree movefor linked worktrees, andgit worktree repairafter a manual move. Newer Git versions (2.48 and later) can store relative paths withworktree.useRelativePaths, which makes moves of the whole tree more robust. - Mixing environments. A worktree created by Windows Git and used from WSL (or the reverse) stores paths the other side cannot read. Create and use each worktree in one environment.
- Old Git.
git worktree addarrived in Git 2.5.moveandremovecame in 2.17, andrepairin 2.30. Checkgit --versionon build servers and in containers before relying on a newer subcommand. - Tools. Some IDEs, file watchers and Git GUIs assume
.gitis a directory. They may mis-detect a linked worktree, show the wrong branch, or index the same code twice if you open both worktrees in one window.
Summary table
| Symptom | Cause | Fix |
|---|---|---|
| "already checked out at ..." | One branch, one worktree | Use that worktree, free it, or branch from it |
| `git branch -d` refused | Branch checked out somewhere | Switch that worktree away first |
| Missing `.env` or dependencies | Untracked and ignored files are not shared | Run a per-worktree setup script |
| Wrong stash popped | Stash is shared | Name stashes, or avoid stashing |
| `remove` refuses | Dirty tree, lock or submodules | Inspect, then `--force` if safe |
| Commits gone after removal | Detached HEAD reflog deleted with worktree | Create a branch first |
| Worktree broken after a move | Absolute paths out of date | `git worktree repair` |
When a second clone is better
Choose a separate clone when you need fully independent configuration and hooks, when a superproject uses submodules heavily, when one environment cannot see the other's paths, or when a tool does not understand linked worktrees. A clone costs more disk and needs its own fetches, but nothing is shared, so nothing can surprise you.
Exercise
You run git worktree add ../app-fix hotfix/crash, which succeeds. Later you delete the ../app-fix folder in your file manager. The next day git switch hotfix/crash in the main directory says the branch is already checked out. Why does this happen, and what is the fix? Also, what must you check first if that worktree had been created with --detach and you had made commits in it?
Show answer
The folder is gone, but the worktree's metadata remains in .git/worktrees/, so Git still treats the branch as checked out there. Run git worktree prune (or git worktree remove before deleting next time) to delete the stale entry, and then the switch works. For a detached worktree, the commits are reachable only from that worktree's HEAD reflog, which is deleted with the worktree. Before removing it, create a branch there with git switch -c <name> so the commits stay reachable.
Exercise
A teammate says: "I created a worktree for a long task and the app won't start there, but it works in the main checkout. The code is identical." Name two likely causes related to this chapter and one way to prevent each.
Show answer
First, untracked and ignored files such as .env were not copied, because new worktrees contain only tracked files. Prevent this with a setup script that copies the needed files. Second, dependencies such as node_modules or a virtual environment are not shared. Prevent this by installing them in each worktree, for example with npm ci. Port collisions with a server running in the other worktree are a valid alternative cause. Prevent that by configuring a different port per worktree.