gitbutlerapp/gitbutler · error · Error
No branch selected!
Error message
No branch selected!
What it means
Thrown in the new-commit flow when finalBranchName is falsy after stack resolution. The name normally comes from the draft branch name state (uiState.global.draftBranchName) or from the freshly created stack's heads[0].name; when both are absent there is no head branch to commit onto.
Source
Thrown at apps/desktop/src/components/commit/NewCommitView.svelte:85
const parentId = commitAction?.parentCommitId;
const insertBelow = commitAction?.insertBelow;
if (!finalStackId) {
const stack = await createNewStack({
projectId,
branch: { name: finalBranchName, order: 0 },
});
finalStackId = stack.id ?? undefined;
finalBranchName = stack.heads[0]?.name; // Updated to access the name property
uiState.global.draftBranchName.set(undefined);
}
if (!finalStackId) {
throw new Error("No stack selected!");
}
if (!finalBranchName) {
throw new Error("No branch selected!");
}
// Run commit-msg hook if hooks are enabled
let finalMessage = message;
if ($runCommitHooks) {
const messageHookResult = await runMessageHook({ projectId, message });
if (messageHookResult?.status === "failure") {
showWarning("Commit message hook failed", messageHookResult.error);
return;
} else if (messageHookResult?.status === "message") {
finalMessage = messageHookResult.message;
}
}
const worktreeChanges = await uncommittedService.worktreeChanges(projectId, stackId);
// Get current editor mode from the component instance
const isRichTextMode = input?.isRichTextMode?.() || false;View on GitHub (pinned to caf1f223d3)
Solutions
- Provide/confirm a branch name in the commit form before saving (pick a target branch in the UI).
- Check that the target stack actually has head branches; if it is empty, delete it and let GitButler create a fresh one.
- In code, validate stack.heads[0]?.name right after createNewStack and retry or surface the error there.
- Reload the project view so branch state is re-synced from the backend.
Example fix
// before
if (!finalBranchName) {
throw new Error("No branch selected!");
}
// after
if (!finalBranchName) {
showWarning("No branch to commit to", "The stack has no head branches; create a branch first");
return;
} Defensive patterns
Strategy: validation
Validate before calling
const draftName = $uiState.global.draftBranchName ?? stack.heads[0]?.name;
if (!draftName) {
showWarning("No branch to commit to", "Create a branch in this stack first");
return;
} Type guard
function hasBranchName(b: string | null | undefined): b is string {
return typeof b === "string" && b.trim().length > 0;
} Try / catch
try {
await createCommit(/* ... */);
} catch (e) {
if (e instanceof Error && e.message === "No branch selected!") {
showToast({ message: "Pick a target branch, then commit again", style: "danger" });
} else throw e;
} Prevention
- Require a non-empty branch name in the commit form before enabling save.
- After createNewStack, verify stack.heads[0]?.name exists before proceeding.
- Clear draft-branch UI state only after confirming the replacement name.
When it happens
Trigger: Committing with no draft branch name set and the existing/created stack exposing an empty heads array (heads[0]?.name is undefined); stack exists with an id but zero head branches.
Common situations: A stack left in a headless state after all its branches were deleted or merged; the draft-branch state was cleared (uiState.global.draftBranchName.set(undefined)) while the commit form stayed open; backend returned a stack DTO without heads.
Related errors
- No branch selected!
- No stack selected!
- Please sign in to use GitButler's AI API
- Please provide a valid API key for your selected AI service
- Not currently on a gitbutler/* branch.
AI-assisted analysis of gitbutlerapp/gitbutler@caf1f223d3 (2026-08-20).
Data as JSON: /api/errors/77df7467483eb584.
Report an issue: GitHub.