gitbutlerapp/gitbutler · error

Could not create branch

Error message

Could not create branch '{}'

What it means

Raised during branch rename when creating the new reference with repo.reference(..., PreviousValue::MustNotExist) fails. The error is wrapped with context 'Could not create branch <name>' and the code then attempts to restore the old ref; if restoration also fails the create_err is returned to the caller.

Solutions

  1. Choose a target branch name that does not already exist
  2. Remove the stale leftover reference from an earlier failed rename (git branch -D or git update-ref -d)
  3. Use a valid ref name (no spaces, no trailing '.lock', no '..')
  4. Check case-collisions on case-insensitive filesystems
Defensive patterns

Strategy: try-catch

Validate before calling

if (await api.branchExists(newName)) throw new Error(`branch '${newName}' already exists`);

Try / catch

try { api.renameBranch(old, new); } catch (e) { if (String(e).includes('Could not create branch')) { console.error('target name taken or invalid:', new); } throw e; }

Prevention

When it happens

Trigger: Renaming a branch to a name that already exists as a reference (MustNotExist violated), an invalid ref name, or an I/O error writing the new ref file.

Common situations: User picks a target name that already exists in the repo, uses invalid characters or casing that collides on case-insensitive filesystems (Windows/macOS), or a stale ref left from a previously failed rename.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/a6d68b89cc4057f4. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-api/src/branch.rs:1411

            if let Err(delete_err) = old_reference.delete() {
                if let Some(backup) = backup_reference.take()
                    && let Err(err) = backup.delete()
                {
                    warn!(
                        ?err,
                        "failed to remove branch-rename recovery ref after source deletion failed"
                    );
                }
                return Err(anyhow::Error::new(delete_err)
                    .context(format!("Could not delete branch '{}'", ref_name.as_bstr())));
            }
            if let Err(create_err) = repo.reference(
                new_ref.as_ref(),
                target_id,
                PreviousValue::MustNotExist,
                "rename branch",
            ) {
                let create_err = anyhow::Error::new(create_err)
                    .context(format!("Could not create branch '{}'", new_ref.as_bstr()));
                match repo.reference(
                    ref_name.as_ref(),
                    target_id,
                    PreviousValue::MustNotExist,
                    "restore branch after failed rename",
                ) {
                    Ok(_) => {
                        if let Some(backup) = backup_reference.take()
                            && let Err(err) = backup.delete()
                        {
                            warn!(
                                ?err,
                                "failed to remove branch-rename recovery ref after restoring source"
                            );
                        }
                        return Err(create_err);
                    }

View on GitHub (pinned to 58e5313667)