GitoxideLabs/gitoxide · error

is checked out in another worktree

Error message

{} is checked out in another worktree

What it means

The branch that should be made available is currently checked out in another worktree of the same repository. Git forbids two worktrees having the same branch checked out, so the library's `ensure_worktree_does_not_own_branch` bails when a linked worktree's HEAD referent matches the branch. Attaching/creating that branch ref would violate git's worktree rules.

Solutions

  1. Remove or detach the conflicting worktree: `git worktree remove <path>` or `git -C <path> checkout --detach`.
  2. Run `git worktree prune` to clean up stale worktree metadata if the directory is gone.
  3. Pick a different branch name for the remembered state.
  4. List worktrees with `git worktree list` to find which one holds the branch.

Example fix

$ git worktree list
/tmp/wt  1a2b3c4 [feature]  # offending
$ git worktree remove /tmp/wt
Defensive patterns

Strategy: validation

Validate before calling

for wt in repo.worktrees()?.iter() {
    if let Ok(head) = wt.repository().head() {
        if head.referent_name() == Some(branch) {
            return Err(anyhow!("branch in use by worktree"));
        }
    }
}

Type guard

fn branch_is_free(repo: &gix::Repository, branch: &gix::refs::FullNameRef) -> bool {
    repo.worktrees()
        .map(|wts| {
            wts.iter().all(|wt| {
                wt.repository()
                    .ok()
                    .and_then(|r| r.head().ok())
                    .and_then(|h| h.referent_name().map(|n| n != branch))
                    .unwrap_or(true)
            })
        })
        .unwrap_or(false)
}

Try / catch

match result {
    Err(e) if e.to_string().contains("checked out in another worktree") => {
        eprintln!("run `git worktree list` and remove/detach the conflicting worktree");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling ensure_branch_is_available (e.g. during attach or perform) while a linked worktree exists whose HEAD symbolic name equals the target branch, detected via `head.referent_name() == Some(branch)`.

Common situations: Developers using `git worktree add` for parallel work; CI sharing a repo directory; the branch left checked out in a stale worktree directory that was never pruned.

Related errors


AI-assisted analysis of GitoxideLabs/gitoxide@e73179060b (2026-09-08). Data as JSON: /api/errors/948df55d2fd8b3e5. Report an issue: GitHub.

Appendix: source

Thrown at gix-tix/src/edit/time_travel.rs:705

        {
            continue;
        }
        ensure_worktree_does_not_own_branch(
            proxy
                .into_repo_with_possibly_inaccessible_worktree()
                .context("could not inspect a linked worktree while checking the remembered branch")?,
            branch,
        )?;
    }
    Ok(())
}

fn ensure_worktree_does_not_own_branch(worktree: gix::Repository, branch: &gix::refs::FullNameRef) -> Result<()> {
    let head = worktree
        .head()
        .context("could not inspect another worktree HEAD while checking the remembered branch")?;
    if head.referent_name() == Some(branch) {
        anyhow::bail!("{} is checked out in another worktree", branch.shorten());
    }
    Ok(())
}

pub(crate) fn perform(
    repository_path: &Path,
    bare: bool,
    selected: ObjectId,
    graph: &history::HistoryGraph,
    review_roots: &[ObjectId],
    revisions: &[OsString],
    include_worktrees: bool,
) -> Result<Perform> {
    perform_reporting_rebased(
        repository_path,
        bare,
        selected,
        graph,

View on GitHub (pinned to e73179060b)