{"record":{"id":"b4c1d564c3c12d56","repo":"windmill-labs/windmill","slug":"could-not-get-git-branch-name","errorCode":null,"errorMessage":"Could not get git branch name","messagePattern":"Could not get git branch name","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/workspace/fork.ts","lineNumber":42,"sourceCode":"\nasync function createWorkspaceFork(\n  opts: GlobalOptions & {\n    createWorkspaceName: string | undefined;\n    color: string | undefined;\n    datatableBehavior: string | undefined;\n    fromBranch: string | undefined;\n    yes: boolean | undefined;\n  },\n  workspaceName: string | undefined,\n  workspaceId: string | undefined = undefined,\n) {\n  if (!isGitRepository()) {\n    throw new Error(\"You can only create forks within a git repo. Forks are tracked with git and synced to your instance with the git sync workflow.\");\n  }\n\n  const currentBranch = getCurrentGitBranch()\n  if (!currentBranch) {\n    throw new Error(\"Could not get git branch name\");\n  }\n\n  const config = await readConfigFile({ warnIfMissing: false });\n  const originalBranchIfForked = getOriginalBranchForWorkspaceForks(currentBranch);\n\n  // A \"base branch\" is one we must not rename onto a fork branch: mapped to a\n  // workspace in wmill.yaml, or a conventional default (main/master).\n  const isBaseBranch = (branch: string): boolean =>\n    branch === \"main\" ||\n    branch === \"master\" ||\n    findWorkspaceByGitBranch(config.workspaces, branch) !== undefined;\n\n  // Decide the base branch the fork links to, and whether to rename the\n  // current working branch onto the fork branch. Auto-detected from where you\n  // are; `--from-branch` is the explicit/non-interactive override.\n  let clonedBranchName: string;\n  let renameCurrent: boolean;\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/cli/src/commands/workspace/fork.ts#L24-L60","documentation":"After confirming the directory is a git repo, the CLI calls `getCurrentGitBranch()` (git branch --show-current / symbolic-ref). If git returns an empty string there is no current branch to anchor the fork to, so the command throws. The most common cause is a detached HEAD state, but an unborn branch (fresh repo with no commits) also yields no branch name.","triggerScenarios":"Running `wmill workspace fork` while HEAD is detached (git checkout <sha>/<tag>), or on a repo with no commits yet (unborn initial branch).","commonSituations":"CI checkouts pinned to a commit SHA; switching to a tag or specific commit to reproduce a bug; `git checkout --detach`; brand-new `git init` repo with zero commits.","solutions":["Check out a named branch: `git checkout -b <branch>` (or `git switch <branch>`)","If detached intentionally, create a branch first: `git switch -c my-fork-base`","In a fresh repo, make an initial commit so the initial branch exists"],"exampleFix":"// before\ngit checkout v1.2.3   # detached HEAD\nwmill workspace fork   # Error: Could not get git branch name\n// after\ngit switch -c work-on-v1.2.3\nwmill workspace fork","handlingStrategy":"validation","validationCode":"import { execSync } from 'node:child_process';\nconst branch = execSync('git branch --show-current', { encoding: 'utf8' }).trim();\nif (!branch) throw new Error('Detached HEAD or unborn branch — check out a named branch before forking.');","typeGuard":"function hasNamedBranch(out: string | null): out is string {\n  return typeof out === 'string' && out.length > 0;\n}","tryCatchPattern":"try {\n  await createWorkspaceFork(opts, name);\n} catch (e) {\n  if ((e as Error).message === 'Could not get git branch name') {\n    console.error('You are in detached HEAD state; run `git switch -c <branch>` first.');\n  } else throw e;\n}","preventionTips":["Never run fork commands on a detached HEAD or tag checkout","In CI, create a named branch after SHA checkouts: `git switch -c ci-run`","Make an initial commit immediately after `git init`"],"tags":["git","detached-head","cli"],"backgroundTag":"git-detached-head","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"}