gitbutlerapp/gitbutler · error

Invalid branch name

Error message

Invalid branch name: {err}

What it means

Raised by `normalize_local_branch_ref` (crates/but/src/command/branch/update.rs:164) when the normalized branch name fails `FullName::try_from`. Only local branches under `refs/heads/` are supported; any other syntax produces an invalid full reference name and the underlying git validation error is interpolated into the message.

Solutions

  1. Check the branch name against `git check-ref-format --branch <name>` and fix invalid characters
  2. Use the short branch name (e.g. `feature/foo`) rather than a full ref path
  3. For remote branches, create a matching local branch first — only `refs/heads/` refs are supported
  4. Quote the branch name in the shell to avoid stray characters being injected

Example fix

// before
but branch update "feature..foo"   // invalid ref name
// after
but branch update "feature-foo"
Defensive patterns

Strategy: validation

Validate before calling

// validate like git does before invoking the command
let out = std::process::Command::new("git")
    .args(["check-ref-format", "--branch", branch])
    .status()?;
if !out.success() { return Err(format!("invalid branch name: {branch}")); }

Type guard

fn is_valid_local_branch_name(name: &str) -> bool {
    !name.starts_with("refs/") && gix::refs::FullName::try_from(format!("refs/heads/{name}")).is_ok()
}

Try / catch

match cmd_branch_update(branch) {
    Err(e) if e.to_string().contains("Invalid branch name") => eprintln!("use a valid branch name: git check-ref-format --branch"),
    other => other?,
}

Prevention

When it happens

Trigger: Calling the branch update command with a name that normalizes to an invalid ref — containing spaces, `..`, `~`, `^`, `:`, control characters, or starting with `refs/` but pointing outside `refs/heads/` (which is rejected earlier).

Common situations: Typing a branch name with illegal characters in the CLI; passing `refs/remotes/origin/foo` or other non-heads refs; shell quoting issues injecting stray characters; overly long branch names.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at crates/but/src/command/branch/update.rs:164

                reference.name.shorten()
            )
        }
        _ => bail!(
            "Expected a local branch, but '{}' is not under refs/heads/",
            reference.name.shorten()
        ),
    }
}

fn normalize_local_branch_ref(branch: &str) -> anyhow::Result<FullName> {
    let full = if branch.starts_with("refs/heads/") {
        branch.to_owned()
    } else if branch.starts_with("refs/") {
        bail!("Only local branches under refs/heads/ are supported");
    } else {
        format!("refs/heads/{branch}")
    };
    FullName::try_from(full).map_err(|err| anyhow::anyhow!("Invalid branch name: {err}"))
}

fn output_apply_result(
    branch_ref: &FullNameRef,
    divergence: &but_workspace::branch::IntegrationDivergenceDisplay,
    dry_run: bool,
    verbose: bool,
    result: IntegrateBranchResult,
    out: &mut OutputChannel,
) -> anyhow::Result<()> {
    if let Some(out) = out.for_human() {
        if dry_run {
            write!(
                out,
                "{}",
                format_dry_run(divergence, branch_ref, &result, verbose)
            )?;
        } else {

View on GitHub (pinned to 58e5313667)