{"record":{"id":"260c1058c257ebed","repo":"jesseduffield/lazygit","slug":"cannot-merge-branch-in-detached-head-state-you-mi","errorCode":null,"errorMessage":"Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on","messagePattern":"Cannot merge branch in detached head state\\. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/gui/controllers/helpers/merge_and_rebase_helper.go","lineNumber":520,"sourceCode":"\n\ttitle := utils.ResolvePlaceholderString(\n\t\tlo.Ternary(self.c.Modes().MarkedBaseCommit.GetHash() != \"\",\n\t\t\tself.c.Tr.RebasingFromBaseCommitTitle,\n\t\t\tself.c.Tr.RebasingTitle),\n\t\tmap[string]string{\n\t\t\t\"checkedOutBranch\": checkedOutBranchName,\n\t\t},\n\t)\n\n\treturn self.c.Menu(types.CreateMenuOptions{\n\t\tTitle: title,\n\t\tItems: menuItems,\n\t})\n}\n\nfunc (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) error {\n\tif self.c.Git().Branch.IsHeadDetached() {\n\t\treturn errors.New(\"Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on\")\n\t}\n\tcheckedOutBranchName := self.c.Model().Branches[0].Name\n\tif checkedOutBranchName == refName {\n\t\treturn errors.New(self.c.Tr.CantMergeBranchIntoItself)\n\t}\n\n\twantFastForward, wantNonFastForward := self.fastForwardMergeUserPreference()\n\tcanFastForward := self.c.Git().Branch.CanDoFastForwardMerge(refName)\n\n\tvar firstRegularMergeItem *types.MenuItem\n\tvar secondRegularMergeItem *types.MenuItem\n\tvar fastForwardMergeItem *types.MenuItem\n\n\tif !wantNonFastForward && (wantFastForward || canFastForward) {\n\t\tfirstRegularMergeItem = &types.MenuItem{\n\t\t\tLabel:   self.c.Tr.RegularMergeFastForward,\n\t\t\tOnPress: self.RegularMerge(refName, git_commands.MERGE_VARIANT_REGULAR),\n\t\t\tKeys:    menuKey('m'),","sourceCodeStart":502,"sourceCodeEnd":538,"githubUrl":"https://github.com/jesseduffield/lazygit/blob/c477a2959b229fbf3284be0d4d2904ab61ec3c94/pkg/gui/controllers/helpers/merge_and_rebase_helper.go#L502-L538","documentation":"Hardcoded (non-i18n) error returned by MergeRefIntoCheckedOutBranch when IsHeadDetached() is true: merging requires a named checked-out branch to merge INTO, and a detached HEAD (a raw commit or a remote-tracking ref checked out directly) gives git no branch to update, so lazygit blocks the operation before invoking git and hints at checking out a local branch.","triggerScenarios":"Selecting a branch (or ref) and pressing the merge key (default 'M') while HEAD is detached — after checking out a commit hash, a tag, or a remote branch such as origin/main without a local copy.","commonSituations":"Inspecting an old commit then trying to merge into it; CI/scratch worktrees created in detached state; repos cloned with --no-checkout where HEAD was never attached.","solutions":["Check out a local branch first: 'git checkout -b <name>' or select the local branch in lazygit's Branches panel and press space.","If you meant to update a remote-tracking ref, fetch instead of merge.","Retry the merge afterwards."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before merging:\nif self.c.Git().Branch.IsHeadDetached() {\n    // prompt to check out a local branch first\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Attach HEAD to a local branch (space in Branches panel) before merge workflows","Avoid checking out remote branches directly; create a local tracking branch"],"tags":["git","merge","detached-head","guard","untranslated"],"backgroundTag":null,"analyzedSha":"c477a2959b229fbf3284be0d4d2904ab61ec3c94","analyzedAt":"2026-08-15T08:34:32.451Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}