{"record":{"id":"9823974422f8e6bc","repo":"gitbutlerapp/gitbutler","slug":"cannot-apply-non-existing-branch-branch","errorCode":null,"errorMessage":"Cannot apply non-existing branch '{branch}'","messagePattern":"Cannot apply non-existing branch '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/apply.rs","lineNumber":582,"sourceCode":"        set_head_to_reference(\n            repo,\n            new_head_id,\n            // Point HEAD at the workspace ref whenever it isn't already there (covers creating the\n            // ref and switching off a directly-checked-out branch).\n            (!head_on_workspace_ref).then_some(workspace_ref_name_to_update.as_ref()),\n        )?;\n        return Ok(Outcome {\n            workspace: graph.into_workspace()?,\n            status: OutcomeStatus::Applied,\n            workspace_ref_created: needs_ws_ref_creation,\n            workspace_merge: None,\n            conflicting_stacks: Vec::new(),\n            applied_branches,\n        });\n    }\n    // We will want to merge, but be sure the branch exists, can't apply non-existing.\n    if branch_ref.is_none() && !incoming_branch_is_remote_tracking_without_local_tracking {\n        bail!(\n            \"Cannot apply non-existing branch '{branch}'\",\n            branch = branch.shorten()\n        );\n    }\n\n    let existing_stacks_superseded_by_branch =\n        find_superseded_stacks(branch.as_ref(), &ws, &mut ws_md);\n    // At this point, the workspace-metadata already knows the new branch(es), but the workspace itself\n    // doesn't see one or more of to-be-applied branches (to become stacks).\n    // These are, however, part of the graph by now, and we want to try to create a workspace\n    // merge.\n    let mut in_memory_repo = repo.clone().for_tree_diffing()?.with_object_memory();\n    let mut merge_result = WorkspaceCommit::from_new_merge_with_metadata(\n        filter_superseded_metadata_stacks(\n            ws_md.stacks.iter(),\n            &existing_stacks_superseded_by_branch,\n        ),\n        filter_superseded_anon_stacks(","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/apply.rs#L564-L600","documentation":"`Workspace::apply` cannot apply a branch that does not exist as a reference. If no branch ref was resolved and the incoming branch is not a remote-tracking branch without a local tracking branch (a case that can be materialized), the library bails. This prevents creating phantom stacks from a typo or deleted branch.","triggerScenarios":"Calling apply with a branch name that is neither a local ref nor derivable from remote-tracking refs — e.g. the branch was deleted, was never fetched, or the name is misspelled.","commonSituations":"Typo in branch name in a script; branch deleted on the remote and locally pruned; stale automation referencing renamed branches; applying before `git fetch` brought the remote branch in.","solutions":["Verify the branch exists: `git rev-parse --verify refs/heads/<name>` before applying","Fetch the remote if the branch only exists remotely (`git fetch origin <name>`) so a local tracking branch can be created","Correct the branch name in the calling code/script"],"exampleFix":"// before\nws.apply(meta, \"feautre-x\", ...)?;\n// after\nif repo.find_reference(&format!(\"refs/heads/feautre-x\")).is_err() {\n    eprintln!(\"branch feautre-x does not exist locally\");\n    return;\n}\nws.apply(meta, \"feature-x\", ...)?;","handlingStrategy":"validation","validationCode":"if repo.try_find_reference(&format!(\"refs/heads/{name}\")).is_err()\n    && repo.try_find_reference(&format!(\"refs/remotes/*/{name}\")).is_err() {\n    return Err(format!(\"branch {name} does not exist locally or remotely\"));\n}","typeGuard":"fn branch_exists(repo: &gix::Repository, name: &str) -> bool {\n    repo.try_find_reference(format!(\"refs/heads/{name}\")).map(|r| r.is_ok()).unwrap_or(false)\n}","tryCatchPattern":"match apply(...) {\n    Err(e) if e.to_string().contains(\"Cannot apply non-existing branch\") => /* fetch or correct name */,\n    Err(e) => return Err(e.into()),\n    Ok(r) => r,\n}","preventionTips":["Verify the branch ref exists before calling apply","Fetch remotes so remote-only branches can be materialized","Avoid hardcoding branch names in scripts; list refs first"],"tags":["git","branch","not-found"],"backgroundTag":"entity-not-found","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"}