gitbutlerapp/gitbutler · error
Stack has no branches
Error message
Stack has no branches
What it means
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.
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.
Example fix
// before but teardown // after but teardown --checkout-to main
Defensive patterns
Strategy: fallback
Validate before calling
let stacks = ...; // before teardown
if !has_explicit_target {
let ok = stacks.first().map(|s| !s.branches.is_empty()).unwrap_or(false);
if !ok { return Err("no stack branch to check out; pass --checkout-to".into()); }
} Try / catch
match result {
Err(e) if e.to_string().contains("Stack has no branches") =>
checkout_explicit("main"),
other => other,
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Aborting due to empty branch name
- Bare repositories are not supported.
- Branch ' ' not found
- Branch ' ' not found when checking for conflicts
- Branch " " has no conflicted commits.
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/107d84b608ef23c0.
Report an issue: GitHub.
Appendix: source
Thrown at crates/but/src/command/legacy/teardown.rs:145
let stacks = match crate::legacy::workspace::applied_stacks(ctx) {
Ok(stacks) => stacks,
Err(_e) => {
try_stack_fixes(ctx, out)?;
crate::legacy::workspace::applied_stacks(ctx)
.context("Failed to retrieve stacks after attempting fixes")?
}
};
let target_branch_name = if let Some(checkout_to) = checkout_to {
checkout_to
} else {
if let Some(target_stack) = stacks.first() {
target_stack
.branches
.first()
.map(|branch| branch.name.clone())
.ok_or_else(|| anyhow::anyhow!("Stack has no branches"))?
} else {
return Err(bad_input(
"Failed to determine checkout target branch. Specify a target branch with `--checkout-to <branch>`.",
).into());
}
};
if let Some(out) = out.for_human() {
writeln!(
out,
" {}",
t.success
.paint(format!("✓ Will check out: {target_branch_name}"))
)?;
writeln!(out)?;
}
// Uninstall managed hooks before checking out. `uninstall_managed_hooks`View on GitHub (pinned to 58e5313667)