gitbutlerapp/gitbutler · error
Cannot position reference below unborn segment
Error message
Cannot position reference below unborn segment '{}' What it means
When creating a reference positioned below a workspace segment, `create_reference` needs an existing tip commit for that segment to anchor against. If the segment has no commits (unborn — neither a ref-derived tip nor any tip commit by segment id), it cannot anchor the new reference and errors with the shortened ref name.
Solutions
- Commit at least once on the target segment before anchoring a reference below it
- Anchor the reference at a different, existing segment or at a commit id instead
- Wait for/retry after the segment's first commit materializes
- Validate the segment has a tip commit (tip_commit_by_segment_id) before creating the reference
Example fix
// before
if segment.tip_commit_by_segment_id(id).is_none() {
return Err(anyhow!("segment {id} is unborn"));
}
tx.create_reference(&ref_name, anchor)?;
// after
match segment.tip_commit_by_segment_id(id) {
Some(_) => tx.create_reference(&ref_name, anchor)?,
None => tracing::warn!("skipping reference {ref_name}: segment unborn"),
} Defensive patterns
Strategy: validation
Validate before calling
fn segment_is_born(ws: &Workspace, segment_id: SegmentId) -> bool {
ws.tip_commit_by_segment_id(segment_id).is_some()
}
// guard: assert!(segment_is_born(&ws, seg.id)); Try / catch
match tx.create_reference(&name, anchor) {
Err(e) if format!("{e:#}").starts_with("Cannot position reference below unborn segment") => {
tracing::warn!("{e:#}; deferring reference creation");
defer_until_segment_has_commit(&name, anchor)
}
r => r?,
} Prevention
- Create references only after the target segment has at least one commit
- Anchor at an explicit commit id when segments may be unborn
- Check tip_commit_by_segment_id before building segment anchors
When it happens
Trigger: Calling create_reference with an anchor at a segment that has never been committed to (freshly created branch with zero commits, or a segment whose tip lookup fails).
Common situations: Creating references below a just-initialized, empty branch; racing reference creation before the first commit lands on the segment; refs created pointing at unborn HEAD on a new repository.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 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/157ca43815538497.
Report an issue: GitHub.
Appendix: source
Thrown at crates/but-transaction/src/lib.rs:617
but_workspace::branch::create_reference::Anchor::AtReference {
ref_name,
position,
},
),
None,
)
} else {
let oldest_commit_id = segment
.commits
.last()
.map(|commit| commit.id)
.or_else(|| {
workspace
.tip_commit_by_segment_id(segment.id)
.map(|commit| commit.id)
})
.ok_or_else(|| {
anyhow::anyhow!(
"Cannot position reference below unborn segment '{}'",
ref_name.shorten()
)
})?;
(
Some(but_workspace::branch::create_reference::Anchor::AtSegment {
ref_name,
position,
}),
Some(oldest_commit_id),
)
}
}
anchor => (anchor, None),
};
let repo = self.repo().clone();
let branch_stack_orders = self
.innerView on GitHub (pinned to 58e5313667)