{"record":{"id":"107d84b608ef23c0","repo":"gitbutlerapp/gitbutler","slug":"stack-has-no-branches-107d84","errorCode":null,"errorMessage":"Stack has no branches","messagePattern":"Stack has no branches","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but/src/command/legacy/teardown.rs","lineNumber":145,"sourceCode":"\n    let stacks = match crate::legacy::workspace::applied_stacks(ctx) {\n        Ok(stacks) => stacks,\n        Err(_e) => {\n            try_stack_fixes(ctx, out)?;\n            crate::legacy::workspace::applied_stacks(ctx)\n                .context(\"Failed to retrieve stacks after attempting fixes\")?\n        }\n    };\n\n    let target_branch_name = if let Some(checkout_to) = checkout_to {\n        checkout_to\n    } else {\n        if let Some(target_stack) = stacks.first() {\n            target_stack\n                .branches\n                .first()\n                .map(|branch| branch.name.clone())\n                .ok_or_else(|| anyhow::anyhow!(\"Stack has no branches\"))?\n        } else {\n            return Err(bad_input(\n                \"Failed to determine checkout target branch. Specify a target branch with `--checkout-to <branch>`.\",\n            ).into());\n        }\n    };\n\n    if let Some(out) = out.for_human() {\n        writeln!(\n            out,\n            \"  {}\",\n            t.success\n                .paint(format!(\"✓ Will check out: {target_branch_name}\"))\n        )?;\n        writeln!(out)?;\n    }\n\n    // Uninstall managed hooks before checking out. `uninstall_managed_hooks`","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but/src/command/legacy/teardown.rs#L127-L163","documentation":"During `but teardown`, when no explicit checkout target is given, the command falls back to the first stack's first branch as the branch to check out after removing all branches. This error is thrown when that fallback stack exists but contains zero branches, so there is no branch name to clone for the checkout target.","triggerScenarios":"Running `but teardown` without `--checkout-to` when the resolved first stack (stacks.first()) has an empty `branches` vector — i.e., the stack metadata exists but no branch entries are attached to it.","commonSituations":"A stack whose branches were all unassigned/deleted before teardown; stale or corrupted workspace state where stack records exist without branch associations; calling teardown in a repo whose first stack is an empty placeholder.","solutions":["Pass an explicit target with `but teardown --checkout-to <branch>` so the code never relies on the stack's branch list.","Run `but status` to inspect the stacks; if the first stack is empty, delete or skip it before tearing down.","Refresh workspace state (e.g., `but branch list` / re-run the workspace operation) so stack branch metadata is rebuilt, then retry teardown."],"exampleFix":"// before\nbut teardown\n\n// after\nbut teardown --checkout-to main","handlingStrategy":"fallback","validationCode":"let stacks = ...; // before teardown\nif !has_explicit_target {\n    let ok = stacks.first().map(|s| !s.branches.is_empty()).unwrap_or(false);\n    if !ok { return Err(\"no stack branch to check out; pass --checkout-to\".into()); }\n}","typeGuard":null,"tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"Stack has no branches\") =>\n        checkout_explicit(\"main\"),\n    other => other,\n}","preventionTips":["Always pass `--checkout-to <branch>` when scripting teardown.","Inspect `but status` and confirm the first stack has branches before teardown.","Clean up empty stacks before running teardown."],"tags":["git","cli","stack","empty-collection"],"backgroundTag":"empty-result-set","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"}