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 messageView on GitHub (pinned to c0c98d367c)
Solutions
- Create and switch to a feature branch first: createAndCheckoutBranch('feature/...'), then commit.
- Pass { force: true } to createCommit if committing to the default branch is truly intended.
- Check getCurrentBranch() before committing and branch off if it's a default branch.
- 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
- Create and switch to a feature branch as the first step of any automated workflow.
- Check getCurrentBranch() before every commit when enforcement is on.
- Only bypass with force:true as a deliberate, reviewed decision.
- Remember the enforcement list is hardcoded: main, master, develop.
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
- no staged changes to commit
- Failed to fetch tasks from any tag. First error: ${failedTag
- ${authCheck.error}
- projectRoot is required for isGitRepository
- projectRoot is required for getCurrentBranch
AI-assisted analysis of eyaltoledano/claude-task-master@c0c98d367c (2026-08-29).
Data as JSON: /api/errors/476c6aaf5c29f3f8.
Report an issue: GitHub.