gitbutlerapp/gitbutler · error
Refusing to delete last named segment
Error message
Refusing to delete last named segment '{}' as it would leave an anonymous segment What it means
remove_reference() refuses to delete a branch if it is the last named segment in the workspace: deleting it would leave the workspace with only anonymous segments, which breaks the stack model (a workspace must retain at least one named segment). The guard counts segments with ref_info before deleting the local branch.
Solutions
- Abandon/remove the whole workspace instead of deleting its last branch
- Create another named branch in the workspace before deleting the last existing one
- Treat this error as a signal to call the workspace-abandon API rather than remove_reference
Example fix
// before
remove_reference(repo, meta, last_branch_ref) // errors on last named segment
// after
if is_last_named_segment(meta, last_branch_ref) {
abandon_workspace(...)
} else {
remove_reference(repo, meta, last_branch_ref)
} Defensive patterns
Strategy: validation
Validate before calling
let named = workspace.segments.iter().filter(|s| s.ref_info.is_some()).count();
if named < 2 && is_named_segment(ref_name) {
// deleting would leave an anonymous workspace; use abandon instead
} Try / catch
match remove_reference(repo, meta, ref_name) {
Err(e) if e.to_string().contains("last named segment") => abandon_workspace_instead(),
other => other?,
} Prevention
- Count named segments before offering branch deletion in UI
- Route 'delete last branch' intents to workspace abandonment
- Keep at least one named branch per workspace at all times
When it happens
Trigger: Calling remove_reference() to delete the only (or last remaining) named branch segment in a workspace stack - the filtered count of segments with ref_info is below 2.
Common situations: Users deleting the final branch of a workspace in the UI/CLI; cleanup scripts removing all stacked branches one by one and hitting the last one; abandoning a workspace via branch deletion.
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
- Ad-hoc (single-branch) branch moves are not supported…
- Branch ' ' cannot be created: the target commit ( ) already…
- BranchNotFound
- BUG: failed to apply head ref
- Cannot add the target
AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18).
Data as JSON: /api/errors/858c9f228e4b9feb.
Report an issue: GitHub.
Appendix: source
Thrown at crates/but-workspace/src/branch/remove_reference.rs:101
let Some((stack, _segment)) = workspace.find_segment_and_stack_by_refname(ref_name) else {
return Ok(None);
};
if avoid_anonymous_stacks
&& (stack
.segments
.iter()
.map(|s| s.commits.len())
.sum::<usize>()
> 0
&& stack
.segments
.iter()
.filter(|s| s.ref_info.is_some())
.count()
< 2)
{
bail!(
"Refusing to delete last named segment '{}' as it would leave an anonymous segment",
ref_name.shorten()
);
}
let deleted_ref = delete_local_branch(repo, ref_name)?;
let deleted_meta = if keep_metadata {
false
} else {
meta.remove(ref_name)?
};
// Unlikely, hard to test, but can happen.
if !deleted_ref && !deleted_meta {
return Ok(None);
}
View on GitHub (pinned to 58e5313667)