Sub topic 10
Locking, moving and repairing worktrees
This chapter assumes you can create, list and remove linked worktrees with git worktree add, list and remove. It also assumes you know that each linked worktree has a private administrative directory inside the main repository.
Why these commands exist
Each linked worktree is tied to the repository by two pointers. The worktree has a .git file that contains a gitdir: line pointing to .git/worktrees/<name> in the main repository. That directory has a gitdir file that points back to the worktree's .git file.
If either path stops being true, Git cannot connect the two. This happens when a directory is renamed in a file manager, or when a worktree lives on a drive that is not mounted. Three commands cover these cases: lock protects an entry, move relocates a worktree safely, and repair fixes broken pointers.
Locking a worktree
git worktree prune deletes the administrative entry of any worktree whose directory is missing. For a worktree on a removable drive or network share, that is a problem. The drive is only unplugged, but Git sees a missing directory.
A lock prevents this.
git worktree lock --reason "on removable drive" ../myproject-review
git worktree listThe list output shows locked next to that entry. With --porcelain, you also get the reason on a locked line. Git stores the reason in a file named locked inside the worktree's administrative directory.
A locked worktree cannot be pruned, moved or removed. To release it, run:
git worktree unlock ../myproject-reviewRemoval and movement can be forced past a lock, but you must pass --force twice. The doubled flag is deliberate friction, so a lock is not bypassed by accident.
Moving a worktree
Do not rename a linked worktree directory with mv. Use Git, which updates both pointers in one step.
git worktree move ../myproject-feature ../work/myproject-featureSome limits apply:
- You cannot move the main worktree.
- Git refuses to move a worktree that contains submodules unless you pass
--force. - Git refuses to move a locked worktree unless you pass
--forcetwice.
Repairing broken links
If you moved a worktree with mv, or moved the whole main repository, the pointers no longer match. Commands may fail with errors such as "not a git repository". Run git worktree repair, and Git rewrites the pointers to match the current locations.
The path you pass depends on what moved:
# A linked worktree was moved by hand: run from the main worktree
# and pass the new path.
git worktree repair ../work/myproject-feature
# The main repository was moved: run from its new location.
# Git can fix the link from the main repository's side, but it cannot
# find linked worktrees that also moved, so list their new paths.
cd /new/place/myproject
git worktree repair ../myproject-hotfix ../myproject-reviewIf you run repair inside a moved linked worktree, it fixes that worktree's own link. Run it without arguments from the main worktree to check the others.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Worktree vanished from `git worktree list` after unplugging a drive | It was pruned, for example by `git gc` after the expiry period | Lock worktrees on removable media before unplugging |
| `fatal: not a git repository` inside a moved worktree | The `.git` file points to the old repository path | Run `git worktree repair` |
| `cannot move a locked working tree` | The worktree is locked | `git worktree unlock`, then move again |
| Worktree still listed after you deleted its directory | The administrative entry is stale | `git worktree prune` |
Exercise
You moved the directory ../myproject-feature to ../archive/myproject-feature with mv, and now git commands inside it fail. What do you run, and from where, to fix it without recreating the worktree?
Show answer
Run git worktree repair ../archive/myproject-feature from the main worktree. This rewrites the pointers between the repository and the moved worktree. Running git worktree repair inside the moved worktree also works. Next time, use git worktree move so Git updates both pointers itself.
Exercise
You keep a worktree on a USB drive that is not always plugged in. Which command protects its metadata from git worktree prune, and how do you check that it worked?
Show answer
Run git worktree lock --reason "on removable drive" <path>. Then run git worktree list, which shows the entry marked locked. The --porcelain form also shows the reason.