Git Branch Already Used by Worktree? Find the Checkout
Contents
When git switch topic says the branch is already used by a worktree, start with git worktree list --porcelain in the same repository. Find the record containing branch refs/heads/topic and read its worktree path. That path identifies the checkout Git associates with the branch.
A missing path needs one more check before cleanup. A worktree moved with a file manager can look identical in the listing to a worktree that was discarded. If you know where the folder went, repair its registration there.
Check the registered path
git worktree list --porcelainIn a disposable repository on macOS with Git 2.54.0 (Apple Git-157), git switch topic failed while a linked checkout held topic. The linked record contained these lines, with the temporary path shortened and the commit hash omitted:
worktree /tmp/demo/topicbranch refs/heads/topicIf that directory still exists, run git -C "/path/to/topic" status --short --branch to see the branch and any working changes. Continue work in that directory. If you only need to inspect the same commit in a separate directory, git worktree add --detach ../topic-review topic creates a detached checkout at the branch tip. Supply an unused destination. In a separate check, an untracked notes.txt in the original checkout was absent from the detached checkout.
If you need the branch in your current directory, check the other worktree for local changes before switching it to another available branch. Then retry your original switch. The Git status and diff checks for staged changes can help you review that checkout before switching.
Repair a moved checkout
Moving the test checkout from topic to moved left the same switch error pointing at the old path. The record gained this line:
prunable gitdir file points to non-existent locationIn the test, running the following from the main checkout updated the registration. Replace the path with the known new location.
git worktree repair "/path/to/moved"git worktree list --porcelaingit -C "/path/to/moved" status --short --branchThe list now pointed to the moved directory, and status still showed ## topic. A follow-up test with an untracked notes.txt confirmed the file still appeared in status after repair and retained its contents. The branch remains checked out there. If the old path was on a drive or network share that may be offline, reconnect it and check again before pruning.
Preview cleanup for a retired checkout
When the folder is gone for good, preview which registrations Git would clear. The expiration flag includes recently orphaned entries in the preview.
git worktree prune --dry-run --verbose --expire nowIn a second test, the original directory for retired was made unavailable. The same prunable marker appeared, and the preview printed:
Removing worktrees/retired: gitdir file points to non-existent locationProceed only after checking every listed entry. The prune command acts across the repository. In the disposable repository, git worktree prune --expire now cleared the retired registration and git switch retired then succeeded. The local branch was still available because pruning clears worktree administration records.
References:
- Git worktree documentation, including porcelain output, repair, and prune.
- Git logo by Jason Long, licensed under CC BY 3.0.