{"record":{"id":"62cda527e245cf6d","repo":"gitbutlerapp/gitbutler","slug":"cannot-add-the-target-branch-branch-to-its-own","errorCode":null,"errorMessage":"Cannot add the target '{branch}' branch to its own workspace","messagePattern":"Cannot add the target '(.+?)' branch to its own workspace","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/apply.rs","lineNumber":251,"sourceCode":"    workspace: but_graph::Workspace,\n    repo: &gix::Repository,\n    meta: &mut impl RefMetadata,\n    Options {\n        workspace_merge: integration_mode,\n        on_workspace_conflict,\n        workspace_reference_naming,\n        order,\n        new_stack_id,\n        allow_applying_already_applied_branch_when_outside_workspace,\n    }: Options,\n) -> anyhow::Result<Outcome> {\n    let ws = workspace;\n    let new_stack_id = new_stack_id.unwrap_or(generate_new_stack_id);\n    let branch_orig = branch;\n    let (mut branch_ref, mut incoming_branch_is_remote_tracking_without_local_tracking) =\n        (try_find_validated_ref(repo, branch, \"apply\")?, false);\n    if ws.is_branch_the_target_or_its_local_tracking_branch(branch) {\n        bail!(\"Cannot add the target '{branch}' branch to its own workspace\");\n    }\n    let mut branch = branch.to_owned();\n    if branch\n        .category()\n        .is_some_and(|c| c == Category::RemoteBranch)\n    {\n        // TODO(gix): we really want to have a function to return the local tracking branch\n        //            fix this in other places, too.\n        let Some((upstream_branch_name, _remote_name)) =\n            repo.upstream_branch_and_remote_for_tracking_branch(branch.as_ref())?\n        else {\n            // TODO: actually create a local trakcing branch with proper configuration.\n            bail!(\"Couldn't find remote refspecs that would match {branch}\");\n        };\n        // Pretend the upstream branch is also the local tracking name.\n        incoming_branch_is_remote_tracking_without_local_tracking = true;\n        branch = upstream_branch_name;\n        branch_ref = try_find_validated_ref(repo, branch.as_ref(), \"apply\")?;","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/apply.rs#L233-L269","documentation":"`but_workspace::branch::apply` refuses to apply a branch into a workspace when that branch already is the workspace's target branch (or its local tracking branch). Doing so would make a stack its own base, which is an invalid workspace state, so it bails early after `try_find_validated_ref`.","triggerScenarios":"Calling `apply(...)` with `branch` equal to the workspace's target branch name or its local tracking branch, detected via `ws.is_branch_the_target_or_its_local_tracking_branch(branch)`.","commonSituations":"UI/CLI automation that always calls apply on the 'current' branch after a checkout; scripting that iterates all branches including the target; passing an already-applied branch back into apply.","solutions":["Check before calling: skip apply when `ws.is_branch_the_target_or_its_local_tracking_branch(branch)` is true.","Pass the branch you actually intend to integrate instead of the workspace target branch.","If the branch is already the target, no apply is needed — treat it as a no-op in the caller."],"exampleFix":"// before\nws.branch_apply(repo, &branch, meta, None, workspace, opts).await?;\n// after\nif !ws.is_branch_the_target_or_its_local_tracking_branch(&branch) {\n    ws.branch_apply(repo, &branch, meta, None, workspace, opts).await?;\n}","handlingStrategy":"validation","validationCode":"if ws.is_branch_the_target_or_its_local_tracking_branch(&branch) {\n    return Ok(()); // nothing to do: branch already is the target\n}\nws.branch_apply(repo, &branch, meta, None, workspace, opts).await?;","typeGuard":null,"tryCatchPattern":"// Rust\nif let Err(e) = ws.branch_apply(repo, &branch, ...).await {\n    if e.to_string().starts_with(\"Cannot add the target\") { return Ok(()); }\n    return Err(e);\n}","preventionTips":["Always guard apply calls with the target-branch check.","Filter the target branch out of any scripted 'apply all branches' loops.","Treat applying the target branch as a no-op in UI flows."],"tags":["git","workspace","branch","validation"],"backgroundTag":"invalid-state-transition","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}