{"record":{"id":"8d3ffcaa99271e9d","repo":"windmill-labs/windmill","slug":"git-branch-m-newname-failed-exit-r-status","errorCode":null,"errorMessage":"git branch -m ${newName} failed (exit ${r.status}): ${r.stderr ?? \"\"}","messagePattern":"git branch -m (.+?) failed \\(exit (.+?)\\): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/src/utils/git.ts","lineNumber":44,"sourceCode":"    \"git\",\n    [\"show-ref\", \"--verify\", \"--quiet\", `refs/heads/${branchName}`],\n    { stdio: \"pipe\" },\n  );\n  return r.status === 0;\n}\n\n/**\n * Rename the currently checked-out branch (`git branch -m <newName>`). Used by\n * `wmill workspace fork --from-branch` to turn an existing working branch into\n * the `wm-fork/<base>/<id>` fork branch in place, preserving its commits.\n */\nexport function renameCurrentGitBranch(newName: string): void {\n  const r = spawnSync(\"git\", [\"branch\", \"-m\", newName], {\n    encoding: \"utf8\",\n    stdio: \"pipe\",\n  });\n  if ((r.status ?? 1) !== 0) {\n    throw new Error(\n      `git branch -m ${newName} failed (exit ${r.status}): ${r.stderr ?? \"\"}`,\n    );\n  }\n}\n\nexport function getOriginalBranchForWorkspaceForks(branchName: string | null): string | null {\n  if (!branchName || !branchName.startsWith(WM_FORK_PREFIX)) {\n    return null\n  }\n\n  const start = branchName.indexOf(\"/\") + 1;\n  const end = branchName.lastIndexOf(\"/\");\n\n  if (start < 0 || end < 0 || end - start <= 0) {\n    return null\n  }\n\n  return branchName.slice(start, end)","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/cli/src/utils/git.ts#L26-L62","documentation":"renameCurrentGitBranch runs `git branch -m <newName>` via spawnSync to rename the current branch in place (used by `wmill workspace fork --from-branch` to turn the working branch into a wm-fork/<base>/<id> branch). If git exits non-zero, the helper throws this error including the exit code and git's stderr. It means git itself refused the rename; no fork branch was created.","triggerScenarios":"Calling createWorkspaceFork/renameCurrentGitBranch outside a git repository (exit 128, 'not a git repository'), when newName is empty/invalid as a branch name (exit 128, 'fatal: not a valid branch name'), or any other git-level refusal of `branch -m`.","commonSituations":"Running `wmill workspace fork` outside a git repo or in a bare checkout; a workspace/fork id that produces an invalid branch name (spaces, invalid chars); detached HEAD or unusual git state; git not installed is not this error (that would surface as r.error instead).","solutions":["Run the command from inside a git working tree (`git rev-parse --is-inside-work-tree` to confirm).","Check the stderr in the message for git's specific complaint and fix it (e.g. invalid characters in the generated branch name).","Commit or stash if git state blocks the rename, then retry `wmill workspace fork`.","Verify the target branch name is valid: `git check-ref-format --branch <newName>`.","Ensure git is installed and on PATH."],"exampleFix":"// before: run outside a repo\n$ cd /tmp && wmill workspace fork --from-branch\n// Error: git branch -m wm-fork/main/abc failed (exit 128): fatal: not a git repository\n// after\n$ cd ~/my-project && wmill workspace fork --from-branch","handlingStrategy":"try-catch","validationCode":"import { spawnSync } from \"node:child_process\";\nfunction canRenameBranchHere(): { ok: boolean; reason?: string } {\n  const inRepo = spawnSync(\"git\", [\"rev-parse\", \"--is-inside-work-tree\"], { encoding: \"utf8\" });\n  if (inRepo.status !== 0 || inRepo.stdout?.trim() !== \"true\") return { ok: false, reason: \"not inside a git work tree\" };\n  return { ok: true };\n}","typeGuard":"function isBranchNameValid(name: string): boolean {\n  const r = spawnSync(\"git\", [\"check-ref-format\", \"--branch\", name], { stdio: \"pipe\" });\n  return r.status === 0;\n}","tryCatchPattern":"try {\n  renameCurrentGitBranch(newName);\n} catch (e) {\n  if (String(e).includes(\"git branch -m\")) {\n    console.error(`Branch rename refused: ${e.message}. Are you inside a git repo and is '${newName}' a valid branch name?`);\n  }\n  throw e;\n}","preventionTips":["Always run fork commands from inside a git working tree.","Validate generated fork branch names with `git check-ref-format --branch` before renaming.","Keep the workspace/fork id free of characters git forbids in branch names.","Run `git status` first to ensure a sane repo state (not detached HEAD without a branch to rename)."],"tags":["git","branch","child-process","cli"],"backgroundTag":"git-command-failed","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}