windmill-labs/windmill · error · Error

Could not get git branch name

Error message

Could not get git branch name

What it means

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.

Source

Thrown at cli/src/commands/workspace/fork.ts:42

async function createWorkspaceFork(
  opts: GlobalOptions & {
    createWorkspaceName: string | undefined;
    color: string | undefined;
    datatableBehavior: string | undefined;
    fromBranch: string | undefined;
    yes: boolean | undefined;
  },
  workspaceName: string | undefined,
  workspaceId: string | undefined = undefined,
) {
  if (!isGitRepository()) {
    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.");
  }

  const currentBranch = getCurrentGitBranch()
  if (!currentBranch) {
    throw new Error("Could not get git branch name");
  }

  const config = await readConfigFile({ warnIfMissing: false });
  const originalBranchIfForked = getOriginalBranchForWorkspaceForks(currentBranch);

  // A "base branch" is one we must not rename onto a fork branch: mapped to a
  // workspace in wmill.yaml, or a conventional default (main/master).
  const isBaseBranch = (branch: string): boolean =>
    branch === "main" ||
    branch === "master" ||
    findWorkspaceByGitBranch(config.workspaces, branch) !== undefined;

  // Decide the base branch the fork links to, and whether to rename the
  // current working branch onto the fork branch. Auto-detected from where you
  // are; `--from-branch` is the explicit/non-interactive override.
  let clonedBranchName: string;
  let renameCurrent: boolean;

View on GitHub (pinned to e474e8803c)

Solutions

  1. Check out a named branch: `git checkout -b <branch>` (or `git switch <branch>`)
  2. If detached intentionally, create a branch first: `git switch -c my-fork-base`
  3. In a fresh repo, make an initial commit so the initial branch exists

Example fix

// before
git checkout v1.2.3   # detached HEAD
wmill workspace fork   # Error: Could not get git branch name
// after
git switch -c work-on-v1.2.3
wmill workspace fork
Defensive patterns

Strategy: validation

Validate before calling

import { execSync } from 'node:child_process';
const branch = execSync('git branch --show-current', { encoding: 'utf8' }).trim();
if (!branch) throw new Error('Detached HEAD or unborn branch — check out a named branch before forking.');

Type guard

function hasNamedBranch(out: string | null): out is string {
  return typeof out === 'string' && out.length > 0;
}

Try / catch

try {
  await createWorkspaceFork(opts, name);
} catch (e) {
  if ((e as Error).message === 'Could not get git branch name') {
    console.error('You are in detached HEAD state; run `git switch -c <branch>` first.');
  } else throw e;
}

Prevention

When it happens

Trigger: Running `wmill workspace fork` while HEAD is detached (git checkout <sha>/<tag>), or on a repo with no commits yet (unborn initial branch).

Common situations: 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.

Related errors


AI-assisted analysis of windmill-labs/windmill@e474e8803c (2026-09-03). Data as JSON: /api/errors/b4c1d564c3c12d56. Report an issue: GitHub.