{"record":{"id":"a6d68b89cc4057f4","repo":"gitbutlerapp/gitbutler","slug":"could-not-create-branch","errorCode":null,"errorMessage":"Could not create branch '{}'","messagePattern":"Could not create branch '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-api/src/branch.rs","lineNumber":1381,"sourceCode":"            if let Err(delete_err) = old_reference.delete() {\n                if let Some(backup) = backup_reference.take()\n                    && let Err(err) = backup.delete()\n                {\n                    warn!(\n                        ?err,\n                        \"failed to remove branch-rename recovery ref after source deletion failed\"\n                    );\n                }\n                return Err(anyhow::Error::new(delete_err)\n                    .context(format!(\"Could not delete branch '{}'\", ref_name.as_bstr())));\n            }\n            if let Err(create_err) = repo.reference(\n                new_ref.as_ref(),\n                target_id,\n                PreviousValue::MustNotExist,\n                \"rename branch\",\n            ) {\n                let create_err = anyhow::Error::new(create_err)\n                    .context(format!(\"Could not create branch '{}'\", new_ref.as_bstr()));\n                match repo.reference(\n                    ref_name.as_ref(),\n                    target_id,\n                    PreviousValue::MustNotExist,\n                    \"restore branch after failed rename\",\n                ) {\n                    Ok(_) => {\n                        if let Some(backup) = backup_reference.take()\n                            && let Err(err) = backup.delete()\n                        {\n                            warn!(\n                                ?err,\n                                \"failed to remove branch-rename recovery ref after restoring source\"\n                            );\n                        }\n                        return Err(create_err);\n                    }","sourceCodeStart":1363,"sourceCodeEnd":1399,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/2497b8007aa4a1922dae9a805b32ffe5b5037785/crates/but-api/src/branch.rs#L1363-L1399","documentation":"Second step of but-api's branch rename failed: creating the new ref with PreviousValue::MustNotExist was rejected — most often because a branch with the new name already exists (including case-insensitive collisions on macOS/Windows, or a race where another process created it first). The code then attempts to restore the old branch and remove the backup ref, so state should return to pre-rename; the surfaced error carries the wrapped creation failure.","triggerScenarios":"Renaming onto an existing local branch name; case-only renames on case-insensitive filesystems where the 'new' name still collides with the old ref; concurrent creation of the same ref between the delete and create steps.","commonSituations":"Rename dialogs that do not filter taken names; users renaming 'feature' to 'Feature' on macOS/Windows; simultaneous git branch operations.","solutions":["Pre-check that refs/heads/<new> does not exist (git rev-parse --verify or the API equivalent) before renaming.","For case-only renames, go through a temporary intermediate name.","If it fires anyway, verify with `git branch` that exactly one of old/new exists; if the auto-restore also failed, recreate the missing side manually.","Retry once any competing operation finishes."],"exampleFix":"// before\nrename_branch(&ctx, &old_name, &new_name)?;\n\n// after\nif repo.find_reference(new_ref.as_ref()).is_ok() {\n    anyhow::bail!(\"a branch named '{new_ref}' already exists; choose another name\");\n}\nrename_branch(&ctx, &old_name, &new_name)?;","handlingStrategy":"validation","validationCode":"if repo.find_reference(new_ref.as_ref()).is_ok() {\n    anyhow::bail!(\"a branch named '{new_ref}' already exists; choose another name\");\n}","typeGuard":null,"tryCatchPattern":"match rename_branch(&ctx, &old_name, &new_name) {\n    Err(e) if e.to_string().contains(\"Could not create branch\") => {\n        // old branch was auto-restored; surface 'name taken' to the user\n        Err(anyhow::anyhow!(\"branch name '{new_name}' is already taken\"))\n    }\n    r => r,\n}?","preventionTips":["Filter existing branch names in rename dialogs.","Use a two-step rename via a temporary name for case-only changes on macOS/Windows.","Disable rename controls while other ref mutations are running."],"tags":["git","refs","branch-rename","already-exists","case-sensitivity"],"backgroundTag":"git-branch-already-exists","analyzedSha":"2497b8007aa4a1922dae9a805b32ffe5b5037785","analyzedAt":"2026-08-17T00:30:25.648Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}