Sub topic 10
Locking, moving and repairing worktrees
This chapter assumes you can already create, list and remove linked worktrees. It also assumes you know that git worktree prune deletes stale administrative entries. Here you will learn how Git keeps track of where each worktree lives, how to protect a worktree from pruning, how to relocate one, and how to recover when a directory was moved without telling Git.
How Git remembers where a worktree lives
A linked worktree is held together by two pointers that face each other.
- The worktree directory contains a file named
.git. It is a plain text file, not a directory, and holds one line such asgitdir: /home/ana/src/myproject/.git/worktrees/myproject-hotfix. - The main repository contains an administrative directory at
.git/worktrees/<name>/. Inside it, a file namedgitdirrecords the absolute path of the worktree's.gitfile. Other files there, such asHEADandindex, are the worktree's private state.
You can look at both pointers directly:
cat ../myproject-hotfix/.git
ls .git/worktrees/
cat .git/worktrees/myproject-hotfix/gitdirThe first command prints the gitdir: line. The second lists one entry per linked worktree. The third prints the path of the worktree's .git file.
Both pointers are absolute paths by default. Moving a directory with mv, a file manager or a backup tool breaks one of them, and Git cannot notice that on its own. Newer Git releases (2.48 and later) can store relative paths instead, which survives moves of the whole tree. Check your version with git --version before relying on that.
Locking a worktree
Pruning removes an administrative entry when the worktree directory it points to no longer exists. That is correct after a hand-deleted directory, and harmful when the directory is only temporarily unreachable. Typical cases are a removable drive, a network share or an encrypted volume that is not mounted.
Pruning happens in two ways:
- You run
git worktree prune. git gcprunes entries older thangc.worktreePruneExpire, which defaults to three months. Git runsgcautomatically in the background, so you do not have to trigger it.
A lock prevents both. Lock the worktree and give the reason:
git worktree lock --reason "on removable drive" ../myproject-reviewGit creates a file named locked in the worktree's administrative directory. The reason is stored as its content. You can confirm the lock in two ways:
git worktree list
git worktree list --porcelainThe plain listing adds locked after the entry. The porcelain format prints a locked line, followed by the reason if there is one. Scripts should read the porcelain format, because the human-readable layout may change.
To create a worktree that is locked from the start, pass --lock to add. This avoids a window between creation and locking:
git worktree add --lock --reason "build server checkout" ../myproject-ci mainRemove the lock when you no longer need it:
git worktree unlock ../myproject-reviewA lock does more than stop pruning. It also blocks git worktree move and git worktree remove, which protects against an accidental cleanup script.
Overriding a lock
Sometimes you really do want to move or remove a locked worktree. Git requires the force option twice, which is deliberate friction:
git worktree move --force --force ../myproject-review ../archive/myproject-review
git worktree remove --force --force ../myproject-reviewPassing --force once handles other problems, such as an unclean worktree on remove. A locked worktree needs it twice. If you are unsure why the lock exists, read the reason first with git worktree list --porcelain. Unlocking and then retrying is usually clearer than doubling the force flag.
Moving a worktree with Git
The supported way to relocate a linked worktree is git worktree move. It moves the directory and rewrites both pointers in one step, so nothing is left dangling:
git worktree move ../myproject-feature ../work/myproject-feature
git worktree listThe listing now shows the new path. Branch, index and uncommitted changes are untouched, because they stay in the same directory and administrative entry.
Some rules apply:
- The main worktree cannot be moved with this command. Moving the main repository is a manual operation, covered below.
- A locked worktree needs
--forcetwice. - Git may refuse to move a worktree that contains submodules. In that case, move it by hand and run
git worktree repairafterwards. - The destination parent directory must already exist. Create it first with
mkdir -p ../work.
Run git worktree move from any worktree of the repository. The paths you give are interpreted relative to your current directory, so use git worktree list first if you are unsure of the exact path to name.
Repairing broken links
If you moved a directory with mv or a tool outside Git, one of the two pointers is now wrong. The symptoms depend on which side moved.
| What moved | Symptom | Where to run `repair` |
|---|---|---|
| A linked worktree, with plain `mv` | `git worktree list` run in the main repository shows the old path marked `prunable`. Inside the moved directory, Git still works, because the `.git` file points to a valid entry. | In the moved worktree, or anywhere with the new path as an argument |
| The main repository | Inside a linked worktree: `fatal: not a git repository: /old/path/.git/worktrees/name` | In the main worktree at its new location |
| Several linked worktrees | The main repository lists several `prunable` entries | From any worktree, naming each new path |
The repair command takes optional paths:
git worktree repair
git worktree repair ../work/myproject-feature ../work/myproject-reviewWithout arguments, repair fixes the links that the current location can verify. Run it in the moved worktree and it re-registers that directory with the main repository. Run it in the relocated main repository and it re-creates the back-links from the linked worktrees it already knows about. With path arguments, it additionally looks at each named directory, finds the .git file there, and updates the administrative entry to point at the new path. This is the way to fix several worktrees at once.
git worktree repair was added in Git 2.30. On older versions you would have to edit the gitdir files by hand, which is still possible if you have to.
A worked recovery
Suppose you reorganised your projects folder with a file manager:
git worktree list/home/ana/src/myproject 3f2a1c9 [main]
/home/ana/src/myproject-feature 8b44d02 [feature/search] prunableThe second entry is marked prunable because its directory no longer exists at that path. You then find it at /home/ana/work/myproject-feature. Fix the link from the main repository by naming the new location:
cd /home/ana/src/myproject
git worktree repair /home/ana/work/myproject-feature
git worktree list/home/ana/src/myproject 3f2a1c9 [main]
/home/ana/work/myproject-feature 8b44d02 [feature/search]The prunable annotation is gone, and your uncommitted changes in that directory were never at risk.
Choosing the right command
Two situations are easy to confuse. A directory that is gone for good is a job for prune (or, better, remove before deleting). A directory that exists somewhere else is a job for repair. If you cannot tell which applies, run git worktree list and look for the prunable annotation. Then search for the directory by name before deciding.
Moving the main repository
Git has no command for moving the main worktree. Use the operating system, then repair:
mv ~/src/myproject ~/work/myproject
cd ~/work/myproject
git worktree repair
git worktree listRunning repair in the main worktree reconnects each linked worktree whose .git file still points to the old location. This only works if the linked worktrees themselves stayed where they were. If you moved those as well, give their new paths as arguments in the same command:
git worktree repair ~/work/myproject-feature ~/work/myproject-reviewThe same applies to a bare repository that serves as the central store. Move it, then run repair inside it.
Common mistakes
- Moving first, asking later. Using
mvon a worktree works until the nextgit worktree list,pruneorgc. Prefergit worktree move. - Locking without a reason. A lock with no reason is a puzzle for you in three months and for your teammates now. Always pass
--reason. - Forgetting that a lock hides a stale worktree. A locked worktree is never pruned, so the entry stays forever if the drive is gone for good. Unlock it and run
prunewhen you are sure. - Expecting
repairto find directories by itself. It repairs only links it can verify from where you run it, plus paths you name. Give it the new path. - Assuming a relocated main repository fixes itself. Linked worktrees keep pointing at the old absolute path until you run
repair.
Exercise
You unplug an external drive that holds a linked worktree, and you will be away from it for about six months. What should you do before unplugging, and why?
Show answer
Lock the worktree with a reason, for example git worktree lock --reason "on external drive" /path/to/worktree. With the drive missing, the directory looks deleted, so git gc would prune the administrative entry once it is older than gc.worktreePruneExpire (three months by default). The lock prevents that. After you plug the drive back in, run git worktree unlock if you want normal maintenance to resume.
Exercise
You renamed ~/src/myproject-feature to ~/work/myproject-feature with your file manager. git worktree list in the main repository now shows the old path as prunable. Which command fixes this, where do you run it, and which command must you avoid?
Show answer
Run git worktree repair ~/work/myproject-feature from the main repository (or run git worktree repair with no arguments from inside the moved directory). It rewrites the administrative entry to the new path. Avoid git worktree prune before repairing, because it would delete the entry that holds the worktree's own HEAD and index.
Exercise
git worktree move ../review ../archive/review fails on a worktree that you locked last week. What are two ways to proceed?
Show answer
Either unlock it first with git worktree unlock ../review and then run the move, or override the lock by passing --force twice: git worktree move --force --force ../review ../archive/review. Unlocking is clearer when you want the lock gone afterwards. Read the lock reason in git worktree list --porcelain before deciding.