Sub topic 12
Limitations and caveats
This chapter assumes you know basic Git (commit, branch, switch, merge), the difference between the working directory, the index and the repository history, and how remote-tracking branches work. It also assumes you have used git worktree add, list and remove from earlier chapters. Worktrees solve a real problem, but they have limits. Knowing them early saves you from confusing errors and lost work.
What a worktree does not share
All worktrees share one object database, one set of refs and one configuration. Each worktree has its own working files, its own index and its own HEAD. Only tracked files, as stored in commits, travel between worktrees. Everything else stays where it was created.
That causes the most common surprises:
- Untracked files in one worktree do not appear in another.
- Ignored files, such as
.env, build output and editor settings, are not copied to a new worktree. - Dependency directories such as
node_modulesor a Python virtual environment must be created again in each worktree. - Uncommitted changes and staged changes belong to one worktree only.
A new worktree is therefore a clean checkout. You may need a setup step before it builds. A small script saves repeating it:
#!/usr/bin/env bash
# new-worktree.sh <path> <branch>
set -euo pipefail
path="$1"
branch="$2"
git worktree add "$path" -b "$branch" main
cp .env "$path/.env"
cd "$path"
npm ciCopy only what you need. Secrets in .env files now exist in two places, so remove the worktree when you finish.
One branch, one worktree
A branch can be checked out in only one worktree at a time. Git enforces this because two worktrees committing to the same branch would move its ref underneath each other and leave one index out of date.
git worktree add ../myproject-feature feature/searchIf feature/search is already checked out elsewhere, Git fails with an error like fatal: 'feature/search' is already checked out at '/path/to/other'. You have three options:
- Switch the other worktree to a different branch, then retry.
- Use
git worktree add --detachto get a detachedHEADat that commit. - Create a new branch that starts at the same commit with
-b.
The --force option overrides the check. Avoid it unless you understand the consequences. Commits made in one worktree move the branch, and the other worktree then shows confusing staged changes.
Shared state has costs
Sharing the repository saves disk space and fetch time, but it creates coupling:
git fetchin any worktree updates remote-tracking branches for all of them.- Garbage collection and
git gcact on the shared object database. Git protects objects reachable from any worktree'sHEADand index. - Branch deletion affects every worktree. Git refuses to delete a branch that is checked out in a worktree.
- Repository-level configuration in
.git/configapplies everywhere, unless you enable per-worktree configuration withextensions.worktreeConfig.
A worktree also depends on the main repository. If you move or delete the main repository directory, linked worktrees break because their .git file points to a path that no longer exists.
Tool and workflow caveats
- Submodules. Git's support for submodules in linked worktrees is limited. Each worktree needs its own submodule checkout, and the documentation still describes multiple checkout with submodules as experimental. Test your layout before relying on it.
- Sparse checkout. Sparse-checkout settings are shared by default. Enable
extensions.worktreeConfigif worktrees need different patterns. - Editors and IDEs. Each worktree is a separate project root. Indexes, caches and run configurations are rebuilt for each one.
- Hooks. Hooks come from the shared
.git/hooksdirectory, so they run in every worktree. They must not assume a particular path. - Disk use. Git objects are shared, but each worktree holds a full copy of the checked-out files plus its dependencies. Many worktrees of a large project can use a lot of space.
- Removable and network drives. If a worktree lives on a drive that is sometimes absent,
git worktree prunemay delete its metadata. Usegit worktree lockto prevent that. - Manual deletion. Deleting a worktree directory with
rm -rfleaves stale metadata. Rungit worktree prune, or usegit worktree removenext time.
Checking for problems
Before cleaning up, list your worktrees and look for entries marked prunable or locked:
git worktree list
git worktree prune --dry-run --verboseIf you moved a directory by hand and links broke, git worktree repair can restore them. Run it from the main repository, or pass the new path.
Exercise
You created a worktree for a new branch and ran the build, but it failed because .env and node_modules are missing. The main worktree builds fine. Why does this happen, and what should you do?
Show answer
A new worktree is a clean checkout of tracked files only. Ignored files such as .env and dependency directories like node_modules are not shared or copied. Copy the files you need (for example cp .env) and install dependencies again in the new worktree, for instance with npm ci. A setup script makes this repeatable.
Exercise
git worktree add ../review feature/search fails with "already checked out". Name two ways to get a working copy of that commit without touching the other worktree.
Show answer
Use git worktree add --detach ../review feature/search to check out the commit at a detached HEAD. Alternatively, create a new branch from it with git worktree add -b review-search ../review feature/search. Both avoid the one-branch-per-worktree restriction.