{"record":{"id":"476c6aaf5c29f3f8","repo":"eyaltoledano/claude-task-master","slug":"cannot-commit-to-default-branch-currentbranch","errorCode":null,"errorMessage":"cannot commit to default branch: ${currentBranch}\nPlease create a feature branch or use force option.","messagePattern":"cannot commit to default branch: (.+?)\nPlease create a feature branch or use force option\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/tm-core/src/modules/git/adapters/git-adapter.ts","lineNumber":576,"sourceCode":"\t * await git.createCommit('Add feature', {\n\t *   enforceNonDefaultBranch: true\n\t * });\n\t */\n\tasync createCommit(\n\t\tmessage: string,\n\t\toptions: {\n\t\t\tmetadata?: Record<string, string>;\n\t\t\tallowEmpty?: boolean;\n\t\t\tenforceNonDefaultBranch?: boolean;\n\t\t\tforce?: boolean;\n\t\t} = {}\n\t): Promise<void> {\n\t\t// Check if on default branch and enforcement is requested\n\t\tif (options.enforceNonDefaultBranch && !options.force) {\n\t\t\tconst currentBranch = await this.getCurrentBranch();\n\t\t\tconst defaultBranches = ['main', 'master', 'develop'];\n\t\t\tif (defaultBranches.includes(currentBranch)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`cannot commit to default branch: ${currentBranch}\\n` +\n\t\t\t\t\t\t`Please create a feature branch or use force option.`\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// Check for staged changes unless allowEmpty\n\t\tif (!options.allowEmpty) {\n\t\t\tconst hasStaged = await this.hasStagedChanges();\n\t\t\tif (!hasStaged) {\n\t\t\t\tthrow new Error('no staged changes to commit');\n\t\t\t}\n\t\t}\n\n\t\t// Build commit arguments\n\t\tconst commitArgs: string[] = ['commit'];\n\n\t\t// Add message","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/packages/tm-core/src/modules/git/adapters/git-adapter.ts#L558-L594","documentation":"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.","triggerScenarios":"Calling createCommit(message, { enforceNonDefaultBranch: true }) — directly or via execute()/registerAutopilotCommitTool flows — while HEAD is on main/master/develop and options.force is falsy.","commonSituations":"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.","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)."],"exampleFix":"// before\nawait git.createCommit('msg', { enforceNonDefaultBranch: true }); // throws on main\n// after\nif (['main', 'master', 'develop'].includes(await git.getCurrentBranch())) {\n  await git.createAndCheckoutBranch('feature/new-work');\n}\nawait git.createCommit('msg', { enforceNonDefaultBranch: true });","handlingStrategy":"validation","validationCode":"const DEFAULTS = ['main', 'master', 'develop'];\nconst current = await git.getCurrentBranch();\nif (DEFAULTS.includes(current)) {\n  await git.createAndCheckoutBranch('feature/new-work');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await git.createCommit('msg', { enforceNonDefaultBranch: true });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('cannot commit to default branch')) {\n    // branch off, stage, then retry — or pass force:true if deliberate\n    throw e;\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["git","commit","branch-protection","default-branch"],"backgroundTag":"commit-to-protected-branch","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}