eyaltoledano/claude-task-master · error

cannot commit to default branch: ${currentBranch} Please cre

Error message

cannot commit to default branch: ${currentBranch}
Please create a feature branch or use force option.

What it means

createCommit() enforces a branch-protection policy: when options.enforceNonDefaultBranch is true and options.force is not set, committing while on a default branch ('main', 'master', 'develop') throws this error. It prevents accidental direct commits to protected integration branches. Setting force bypasses the check.

Source

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

	 * await git.createCommit('Add feature', {
	 *   enforceNonDefaultBranch: true
	 * });
	 */
	async createCommit(
		message: string,
		options: {
			metadata?: Record<string, string>;
			allowEmpty?: boolean;
			enforceNonDefaultBranch?: boolean;
			force?: boolean;
		} = {}
	): Promise<void> {
		// Check if on default branch and enforcement is requested
		if (options.enforceNonDefaultBranch && !options.force) {
			const currentBranch = await this.getCurrentBranch();
			const defaultBranches = ['main', 'master', 'develop'];
			if (defaultBranches.includes(currentBranch)) {
				throw new Error(
					`cannot commit to default branch: ${currentBranch}\n` +
						`Please create a feature branch or use force option.`
				);
			}
		}

		// Check for staged changes unless allowEmpty
		if (!options.allowEmpty) {
			const hasStaged = await this.hasStagedChanges();
			if (!hasStaged) {
				throw new Error('no staged changes to commit');
			}
		}

		// Build commit arguments
		const commitArgs: string[] = ['commit'];

		// Add message

View on GitHub (pinned to c0c98d367c)

Solutions

  1. Create and switch to a feature branch first: createAndCheckoutBranch('feature/...'), then commit.
  2. Pass { force: true } to createCommit if committing to the default branch is truly intended.
  3. Check getCurrentBranch() before committing and branch off if it's a default branch.
  4. Update default branch list expectations if your repo uses a different primary branch name (only main/master/develop are enforced).

Example fix

// before
await git.createCommit('msg', { enforceNonDefaultBranch: true }); // throws on main
// after
if (['main', 'master', 'develop'].includes(await git.getCurrentBranch())) {
  await git.createAndCheckoutBranch('feature/new-work');
}
await git.createCommit('msg', { enforceNonDefaultBranch: true });
Defensive patterns

Strategy: validation

Validate before calling

const DEFAULTS = ['main', 'master', 'develop'];
const current = await git.getCurrentBranch();
if (DEFAULTS.includes(current)) {
  await git.createAndCheckoutBranch('feature/new-work');
}

Try / catch

try {
  await git.createCommit('msg', { enforceNonDefaultBranch: true });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('cannot commit to default branch')) {
    // branch off, stage, then retry — or pass force:true if deliberate
    throw e;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling createCommit(message, { enforceNonDefaultBranch: true }) — directly or via execute()/registerAutopilotCommitTool flows — while HEAD is on main/master/develop and options.force is falsy.

Common situations: Automation that forgot to create/switch to a feature branch before committing; a fresh repo that is still on the default branch; developer intentionally committing to main but leaving enforcement enabled.

Related errors


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