eyaltoledano/claude-task-master · error

cannot delete current branch: ${branchName}

Error message

cannot delete current branch: ${branchName}

What it means

deleteBranch() refuses to delete the branch that HEAD currently points to, because git cannot delete the checked-out branch. The adapter compares getCurrentBranch() against the requested name and throws this error before running git branch -d/-D.

Source

Thrown at packages/tm-core/src/modules/git/adapters/git-adapter.ts:504

	 *
	 * @example
	 * await git.deleteBranch('old-feature');
	 * await git.deleteBranch('unmerged-feature', { force: true });
	 */
	async deleteBranch(
		branchName: string,
		options: { force?: boolean } = {}
	): Promise<void> {
		// Check if branch exists
		const exists = await this.branchExists(branchName);
		if (!exists) {
			throw new Error(`branch does not exist: ${branchName}`);
		}

		// Check if trying to delete current branch
		const current = await this.getCurrentBranch();
		if (current === branchName) {
			throw new Error(`cannot delete current branch: ${branchName}`);
		}

		// Delete the branch
		const deleteOptions = options.force
			? ['-D', branchName]
			: ['-d', branchName];
		await this.git.branch(deleteOptions);
	}

	/**
	 * Stages files for commit.
	 *
	 * @param {string[]} files - Array of file paths to stage
	 * @returns {Promise<void>}
	 *
	 * @example
	 * await git.stageFiles(['file1.txt', 'file2.txt']);
	 * await git.stageFiles(['.']); // Stage all changes

View on GitHub (pinned to c0c98d367c)

Solutions

  1. Switch to another branch first (e.g. checkoutBranch('main') or your base branch), then delete the target.
  2. Skip the current branch in cleanup loops: if (current === name) continue.
  3. If deleting the current branch seems necessary, you actually want to switch away — git requires a valid HEAD.

Example fix

// before
await git.deleteBranch('feature/x'); // throws if it's the current branch
// after
if ((await git.getCurrentBranch()) !== 'feature/x') {
  await git.deleteBranch('feature/x');
} else {
  await git.checkoutBranch('main');
  await git.deleteBranch('feature/x');
}
Defensive patterns

Strategy: validation

Validate before calling

const current = await git.getCurrentBranch();
if (current !== 'feature/x') {
  await git.deleteBranch('feature/x');
}

Try / catch

try {
  await git.deleteBranch('feature/x');
} catch (e) {
  if (e instanceof Error && e.message.startsWith('cannot delete current branch')) {
    await git.checkoutBranch('main');
    await git.deleteBranch('feature/x');
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling gitAdapter.deleteBranch('main') (or any branch name equal to getCurrentBranch()), e.g. cleanup logic that computes the branch list without excluding the current one.

Common situations: Batch cleanup of merged branches that includes the branch you're standing on; automation that forgot to switch to a base branch before deleting a feature branch that is currently checked out.

Related errors


AI-assisted analysis of eyaltoledano/claude-task-master@c0c98d367c (2026-08-29). Data as JSON: /api/errors/c779d0772120ec86. Report an issue: GitHub.