gitbutlerapp/gitbutler · error

Branch ' ' cannot be created: the target commit ( ) already…

Error message

Branch '{}' cannot be created: the target commit ({}) already belongs to another branch in the workspace. Each commit can only belong to one branch at a time.

What it means

GitButler enforces that each commit belongs to exactly one branch within a workspace. When creating a reference whose target commit is already claimed by another branch in the same workspace, creation is refused because two branches cannot share ownership of a commit.

Solutions

  1. Create the branch at a commit not owned by any workspace branch (e.g. a new commit on top)
  2. Remove or move the existing branch that owns the commit before creating the new one
  3. Use branch creation from the workspace UI, which picks an unowned commit automatically

Example fix

// before
create_reference(meta, repo, "feature", other_branch_commit_id)?;
// after
let free_commit = repo.commit_create_or_fail(parent, tree, msg)?; // commit not in any segment
create_reference(meta, repo, "feature", free_commit)?;
Defensive patterns

Strategy: validation

Validate before calling

let owned = workspace.all_segments()
    .flat_map(|s| s.commits())
    .any(|c| c.id == target_commit);
if owned {
    return Err("target commit already belongs to a workspace branch");
}

Try / catch

match create_reference(...) {
    Err(e) if e.to_string().contains("already belongs to another branch") => /* pick a different commit or remove the owning branch */,
    Err(e) => return Err(e.into()),
    Ok(r) => r,
}

Prevention

When it happens

Trigger: Calling `create_reference` with a target commit id that is the head (or part) of another branch's segment in the workspace — e.g. reusing an existing commit hash as the new branch's target.

Common situations: Creating a branch from a commit id copied from another branch's log; automation that picks a commit without checking ownership; trying to duplicate a branch target within one workspace.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/abc17d544133900c. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-workspace/src/branch/create_reference.rs:489

        };

        let updated_workspace = graph_with_new_ref.into_workspace()?;
        let has_new_ref_as_standalone_segment = updated_workspace
            .find_segment_and_stack_by_refname(ref_name)
            .is_some();
        let existing_ref_is_in_workspace = existing_ref_target_in_workspace.is_some();
        if !has_new_ref_as_standalone_segment {
            if existing_ref_target_id.is_some()
                && !existing_ref_is_in_workspace
                && !workspace.refname_is_segment(ref_name)
                && workspace.ref_name() != Some(ref_name)
            {
                bail_precondition!(
                    "Reference '{}' already exists and is outside the workspace",
                    ref_name.shorten()
                );
            }
            bail!(
                "Branch '{}' cannot be created: the target commit ({}) already \
                 belongs to another branch in the workspace. Each commit can only \
                 belong to one branch at a time.",
                ref_name.shorten(),
                ref_target_id,
            )
        }

        // Actually apply the changes
        repo.reference(
            ref_name,
            ref_target_id,
            PreviousValue::ExistingMustMatch(gix::refs::Target::Object(ref_target_id)),
            "Dependent branch by GitButler",
        )
        .map_err(|err| {
            if is_not_a_directory_ref_edit_error(&err)
                && let Ok(Some(colliding_ref)) = find_colliding_ref_ancestor(repo, ref_name)

View on GitHub (pinned to 58e5313667)