{"record":{"id":"c779d0772120ec86","repo":"eyaltoledano/claude-task-master","slug":"cannot-delete-current-branch-branchname","errorCode":null,"errorMessage":"cannot delete current branch: ${branchName}","messagePattern":"cannot delete current branch: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/tm-core/src/modules/git/adapters/git-adapter.ts","lineNumber":504,"sourceCode":"\t *\n\t * @example\n\t * await git.deleteBranch('old-feature');\n\t * await git.deleteBranch('unmerged-feature', { force: true });\n\t */\n\tasync deleteBranch(\n\t\tbranchName: string,\n\t\toptions: { force?: boolean } = {}\n\t): Promise<void> {\n\t\t// Check if branch exists\n\t\tconst exists = await this.branchExists(branchName);\n\t\tif (!exists) {\n\t\t\tthrow new Error(`branch does not exist: ${branchName}`);\n\t\t}\n\n\t\t// Check if trying to delete current branch\n\t\tconst current = await this.getCurrentBranch();\n\t\tif (current === branchName) {\n\t\t\tthrow new Error(`cannot delete current branch: ${branchName}`);\n\t\t}\n\n\t\t// Delete the branch\n\t\tconst deleteOptions = options.force\n\t\t\t? ['-D', branchName]\n\t\t\t: ['-d', branchName];\n\t\tawait this.git.branch(deleteOptions);\n\t}\n\n\t/**\n\t * Stages files for commit.\n\t *\n\t * @param {string[]} files - Array of file paths to stage\n\t * @returns {Promise<void>}\n\t *\n\t * @example\n\t * await git.stageFiles(['file1.txt', 'file2.txt']);\n\t * await git.stageFiles(['.']); // Stage all changes","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/packages/tm-core/src/modules/git/adapters/git-adapter.ts#L486-L522","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Switch to another branch first (e.g. checkoutBranch('main') or your base branch), then delete the target.","Skip the current branch in cleanup loops: if (current === name) continue.","If deleting the current branch seems necessary, you actually want to switch away — git requires a valid HEAD."],"exampleFix":"// before\nawait git.deleteBranch('feature/x'); // throws if it's the current branch\n// after\nif ((await git.getCurrentBranch()) !== 'feature/x') {\n  await git.deleteBranch('feature/x');\n} else {\n  await git.checkoutBranch('main');\n  await git.deleteBranch('feature/x');\n}","handlingStrategy":"validation","validationCode":"const current = await git.getCurrentBranch();\nif (current !== 'feature/x') {\n  await git.deleteBranch('feature/x');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await git.deleteBranch('feature/x');\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('cannot delete current branch')) {\n    await git.checkoutBranch('main');\n    await git.deleteBranch('feature/x');\n    return;\n  }\n  throw e;\n}","preventionTips":["Always compare getCurrentBranch() to the deletion target first.","In batch cleanups, filter out the current branch explicitly.","Switch to a stable base branch (main/develop) before running deletion scripts.","Never design flows that require deleting the checked-out branch — git forbids it."],"tags":["git","branch","current-branch"],"backgroundTag":"cannot-delete-current-branch","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}