gitbutlerapp/gitbutler · error

Failed to determine Gerrit target branch

Error message

Failed to determine Gerrit target branch

What it means

In gerrit mode, `gerrit_review_ref` builds the review ref `refs/for/<short-name>` from the workspace target: `ResolvedTarget::resolve_with_perm(...)?.ref_name()` returning `None` triggers this error before any push happens. Without a target ref there is no branch to construct refs/for against.

Source

Thrown at crates/but/src/command/legacy/push.rs:1218

        writeln!(
            out,
            "{} Push succeeded, but review synchronization failed: {}",
            t.sym().warning,
            t.hint.paint(message)
        )?;
    }
    Ok(())
}

fn gerrit_review_ref(
    ctx: &Context,
    perm: &RepoShared,
    repo: &gix::Repository,
) -> anyhow::Result<String> {
    let target_ref_name = workspace_target::ResolvedTarget::resolve_with_perm(ctx, perm)?
        .ref_name()
        .map(ToOwned::to_owned)
        .ok_or_else(|| anyhow::anyhow!("Failed to determine Gerrit target branch"))?;
    let remote_names = repo.remote_names();
    let target_branch =
        but_core::extract_remote_name_and_short_name(target_ref_name.as_ref(), &remote_names)
            .map(|(_, short_name)| short_name.to_string())
            .unwrap_or_else(|| target_ref_name.shorten().to_string());

    Ok(format!("refs/for/{target_branch}"))
}

/// Check if a push of this branch would include any conflicted commits.
/// The push covers the branch and its stack ancestors, so those are checked
/// too. Returns an error if conflicted commits are found.
fn check_for_conflicted_commits(ctx: &Context, branch_name: &str) -> anyhow::Result<()> {
    let stacks = crate::legacy::workspace::applied_stacks_with_expensive_commit_info(ctx)?;

    let repo = ctx.repo.get()?.clone().for_commit_shortening();
    // Find the stack containing this branch.
    for stack in &stacks {

View on GitHub (pinned to caf1f223d3)

Solutions

  1. Set the workspace target branch: `but config target <branch>` (e.g. `but config target origin/main`)
  2. Ensure the target ref exists on a configured remote (`git remote -v`, then `git fetch`)
  3. Re-run the gerrit push

Example fix

# before
but push --wip
# error: Failed to determine Gerrit target branch

# after
but config target origin/main
but push --wip
Defensive patterns

Strategy: validation

Validate before calling

# Gerrit push preflight
[ "$(git config --bool gitbutler.gerritMode)" = "true" ] || { echo 'gerrit mode off' >&2; exit 1; }
but config target   # must report a target branch before refs/for can be built

Prevention

When it happens

Trigger: Running a gerrit push (`gitbutler.gerritMode` enabled) in a workspace whose target branch is unset or unresolvable — target never configured, or target ref missing after a partial clone or restore.

Common situations: Gerrit mode enabled before target setup; workspace metadata restored without the target; fresh clone where onboarding was skipped.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@caf1f223d3 (2026-08-20). Data as JSON: /api/errors/b1bff1b5c2693bd4. Report an issue: GitHub.