gitbutlerapp/gitbutler · error

Target branch not found

Error message

Target branch {target_ref_name} not found

What it means

`decide_land_outcome` resolves the merge target as the remote-tracking ref `refs/remotes/{fetch_remote_name}/{target_branch_name}` and throws this error when that reference does not exist. The land operation intentionally merges onto the remote-tracking state, so the target branch must exist on the configured fetch remote.

Solutions

  1. Run `git fetch <fetch_remote_name>` so the remote-tracking ref exists.
  2. Confirm the remote name and target branch: `git branch -r | grep <target_branch_name>`.
  3. Push the target branch to the remote if it only exists locally.
  4. Fix the project's configured target/fetch remote settings if they point at a stale remote.

Example fix

// before
// assumes origin/main exists
land(target_branch_name = "main", fetch_remote_name = "origin");
// after
repo.remote_at("origin")?; // ensure remote configured
run("git", &["fetch", "origin", "main"])?; // create refs/remotes/origin/main
land(target_branch_name = "main", fetch_remote_name = "origin");
Defensive patterns

Strategy: validation

Validate before calling

// ensure the remote-tracking ref exists before deciding land outcome
let target = format!("refs/remotes/{remote}/{target_branch}");
if repo.try_find_reference(&target)?.is_none() {
    run_git(&["fetch", remote, target_branch])?; // or fail fast
}

Try / catch

if let Err(e) = decide_land(...) {
    if e.to_string().contains("Target branch") && e.to_string().contains("not found") {
        run_git(&["fetch", remote])?; // then retry
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: Calling the land/merge API when the repo has never fetched the target branch from `fetch_remote_name` (no `refs/remotes/<remote>/<target>` ref); the remote name is wrong; the branch does not exist upstream; or the target is configured as a local-only branch with no remote tracking ref.

Common situations: Fresh clone without `git fetch`; remote renamed from `origin`; target branch deleted on the remote; pushing-only remotes where tracking refs were pruned.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at crates/but-api/src/land/merge.rs:46

/// rename-aware merge commit. Conflicts bail here, before anything is pushed or moved.
pub(super) fn decide_land_outcome(
    repo: &gix::Repository,
    branch_name: &str,
    fetch_remote_name: &str,
    target_branch_name: &str,
    no_ff: bool,
) -> anyhow::Result<LandOutcome> {
    let feature_ref_name = format!("refs/heads/{branch_name}");
    let feature_oid = repo
        .try_find_reference(&feature_ref_name)?
        .ok_or_else(|| anyhow::anyhow!("Branch {branch_name} not found"))?
        .into_fully_peeled_id()?
        .detach();

    let target_ref_name = format!("refs/remotes/{fetch_remote_name}/{target_branch_name}");
    let target_oid = repo
        .try_find_reference(&target_ref_name)?
        .ok_or_else(|| anyhow::anyhow!("Target branch {target_ref_name} not found"))?
        .into_fully_peeled_id()?
        .detach();

    // No common ancestor: refuse rather than merge two unrelated histories onto the target.
    let Some(merge_base) = super::merge_base_opt(repo, feature_oid, target_oid)? else {
        bail!(
            "Cannot merge {branch_name}: it shares no history with {fetch_remote_name}/{target_branch_name}"
        );
    };

    if merge_base == feature_oid {
        return Ok(LandOutcome::AlreadyIntegrated);
    }
    if merge_base == target_oid && !no_ff {
        return Ok(LandOutcome::FastForward {
            feature_oid,
            target_oid,
        });

View on GitHub (pinned to 58e5313667)