{"record":{"id":"4bec95d349170418","repo":"gitbutlerapp/gitbutler","slug":"cannot-rename-to-a-branch-with-that-name-already-exists","errorCode":null,"errorMessage":"Cannot rename to '{}': a branch with that name already exists","messagePattern":"Cannot rename to '(.+?)': a branch with that name already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-meta/src/legacy/mod.rs","lineNumber":755,"sourceCode":"                        })\n                    })\n            })\n        else {\n            // No managed metadata for this ref (e.g. an ad-hoc branch); nothing to rename here.\n            return Ok(());\n        };\n\n        // Refuse to rename onto a name that already exists in managed metadata: it would create\n        // duplicate `refs/heads/<name>` heads, which the rest of the code treats as an invariant\n        // violation (see the \"There shouldn't be duplication\" note in `branch()`).\n        let new_exists = self.data().branches.values().any(|stack| {\n            stack.heads.iter().any(|branch| {\n                full_branch_name(branch.name.as_str())\n                    .is_some_and(|full_name| full_name.as_ref() == new_ref_name)\n            })\n        });\n        if new_exists {\n            bail!(\n                \"Cannot rename to '{}': a branch with that name already exists\",\n                new_ref_name.shorten()\n            );\n        }\n\n        let new_short_name = new_ref_name.shorten().to_string();\n        let stack = self\n            .data_mut()\n            .branches\n            .get_mut(&stack_id)\n            .expect(\"stack was just found\");\n        stack.heads[branch_idx].name = new_short_name;\n        self.snapshot.set_changed_to_necessitate_write();\n        Ok(())\n    }\n}\n\n/// Ref metadata backed by legacy TOML for historical workspace metadata and by","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-meta/src/legacy/mod.rs#L737-L773","documentation":"Guard in legacy branch metadata rename: the new name already exists as a head in managed ref metadata, so renaming would produce duplicate refs/heads/<name> heads, which the rest of the code assumes never happens. Raised by rename when the caller picks a colliding target name; choose a unique branch name or delete the existing branch first.","triggerScenarios":"Calling rename with a new_ref_name that matches (after full-name normalization) any existing branch head in the stack.","commonSituations":"Renaming a branch to a name that already exists in the workspace, case-variant duplicates, or reusing a recently deleted-but-recreated name.","solutions":["Pick a different, unused name for the branch","List existing branches and choose a unique name","Delete or rename the conflicting branch first if it is no longer needed"],"exampleFix":"// before\nstack.rename(old, \"feature-x\") // feature-x already exists\n// after\nstack.rename(old, \"feature-x-v2\")","handlingStrategy":"validation","validationCode":"// before renaming, check target uniqueness\nif (stack.heads.some(b => fullName(b.name) === newFullName)) {\n  throw new Error(`Branch ${newName} already exists`);\n}","typeGuard":null,"tryCatchPattern":"// Rust\nmatch stack.rename(old, new_name) {\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        // prompt user for a different name\n    }\n    other => other,\n}","preventionTips":["List existing branch names before choosing a rename target","Use UI autocompletion to detect name collisions","Avoid case-variant duplicates of existing names"],"tags":["git","branch","rename","conflict"],"backgroundTag":"entity-already-exists","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}