From 73ac8fe6c6d5b30d97c4807ffbbb68fe732fe095 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 11:38:29 +0100 Subject: [PATCH] Allow merging into a detached HEAD Pressing the merge key while HEAD is detached shows an error. This also happens during a rebase and during a bisect, because HEAD is detached then. As a result, you can't squash merge another branch at the base of the current one by starting an interactive rebase with `i`. The workaround is to squash merge the branch at the tip and move the commit down. If the merged changes conflict with the branch's own commits, you have to resolve those conflicts twice this way. Remove the check. A merge on a detached HEAD is no riskier than a commit, a cherry-pick or a revert, and lazygit allows all of those there. It is also useful outside of a rebase, for example to check out master as a throwaway detached HEAD and merge several branches into it to see whether they conflict. There is no reason to prevent the user from doing these things. If the merge conflicts during a rebase, the merge takes precedence over the rebase in the working tree state. So continuing goes to the merge first and then to the rebase. Co-Authored-By: Claude Opus 5.5 --- docs-master/Config.md | 2 + pkg/commands/git_commands/branch.go | 7 -- pkg/config/user_config.go | 1 + .../helpers/merge_and_rebase_helper.go | 3 - .../tests/branch/merge_into_detached_head.go | 82 +++++++++++++ .../branch/squash_merge_during_rebase.go | 114 ++++++++++++++++++ pkg/integration/tests/test_list.go | 2 + schema-master/config.json | 2 +- 8 files changed, 202 insertions(+), 11 deletions(-) create mode 100644 pkg/integration/tests/branch/merge_into_detached_head.go create mode 100644 pkg/integration/tests/branch/squash_merge_during_rebase.go diff --git a/docs-master/Config.md b/docs-master/Config.md index c8bb8c5f6..d8de794be 100644 --- a/docs-master/Config.md +++ b/docs-master/Config.md @@ -456,6 +456,8 @@ git: # The commit message to use for a squash merge commit. Can contain # "{{selectedRef}}" and "{{currentBranch}}" placeholders. + # If HEAD is detached, "{{currentBranch}}" is the branch being rebased, or + # "HEAD" if no rebase is in progress. squashMergeMessage: Squash merge {{selectedRef}} into {{currentBranch}} # list of branches that are considered 'main' branches, used when displaying diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index fb32baa74..92d69c06d 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -236,13 +236,6 @@ func (self *BranchCommands) countDifferences(from, to string) (string, error) { return self.cmd.New(cmdArgs).DontLog().RunWithOutput() } -func (self *BranchCommands) IsHeadDetached() bool { - cmdArgs := NewGitCmd("symbolic-ref").Arg("-q", "HEAD").ToArgv() - - err := self.cmd.New(cmdArgs).DontLog().Run() - return err != nil -} - func (self *BranchCommands) Rename(oldName string, newName string) error { cmdArgs := NewGitCmd("branch"). Arg("--move", oldName, newName). diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 140defcf3..095509e07 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -424,6 +424,7 @@ type MergingConfig struct { // Extra args passed to `git merge`, e.g. --no-ff Args string `yaml:"args" jsonschema:"example=--no-ff"` // The commit message to use for a squash merge commit. Can contain "{{selectedRef}}" and "{{currentBranch}}" placeholders. + // If HEAD is detached, "{{currentBranch}}" is the branch being rebased, or "HEAD" if no rebase is in progress. SquashMergeMessage string `yaml:"squashMergeMessage"` } diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go index 84888cadd..f231bf719 100644 --- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go +++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go @@ -603,9 +603,6 @@ func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error { } func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) error { - if self.c.Git().Branch.IsHeadDetached() { - return 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") - } if self.c.Model().Branches[0].Name == refName { return errors.New(self.c.Tr.CantMergeBranchIntoItself) } diff --git a/pkg/integration/tests/branch/merge_into_detached_head.go b/pkg/integration/tests/branch/merge_into_detached_head.go new file mode 100644 index 000000000..148bf7cfe --- /dev/null +++ b/pkg/integration/tests/branch/merge_into_detached_head.go @@ -0,0 +1,82 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var MergeIntoDetachedHead = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Merge and squash merge branches into a detached HEAD", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Git.LocalBranchSortOrder = "alphabetical" + }, + SetupRepo: func(shell *Shell) { + shell. + EmptyCommit("one"). + NewBranch("branch1"). + EmptyCommit("branch1"). + Checkout("master"). + NewBranch("branch2"). + CreateFileAndAdd("file", "content"). + Commit("branch2"). + Checkout("master"). + EmptyCommit("two"). + Checkout("master^0") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("HEAD detached").IsSelected(), + Contains("branch1"), + Contains("branch2"), + Contains("master"), + ). + NavigateToLine(Contains("branch1")). + Press(keys.Branches.MergeIntoCurrentBranch) + + t.ExpectPopup().Menu(). + Title(Equals("Merge")). + Select(Contains("Regular merge (with merge commit)")). + Tooltip(Equals("Merge 'branch1' into 'HEAD', creating a merge commit.")). + Confirm() + + t.Views().Commits(). + Lines( + Contains("◎─╮ Merge branch 'branch1' into HEAD").IsSelected(), + Contains("│ ○ * branch1"), + Contains("○ │ two"), + Contains("○─╯ one"), + ) + + t.Views().Branches(). + Focus(). + NavigateToLine(Contains("branch2")). + Press(keys.Branches.MergeIntoCurrentBranch) + + t.ExpectPopup().Menu(). + Title(Equals("Merge")). + Select(Contains("Squash merge and commit")). + Tooltip(Equals("Squash merge 'branch2' into 'HEAD' as a single commit.")). + Confirm() + + t.Views().Commits(). + Lines( + Contains("Squash merge branch2 into HEAD").IsSelected(), + Contains("Merge branch 'branch1' into HEAD"), + Contains("branch1"), + Contains("two"), + Contains("one"), + ) + + t.Views().Branches(). + Lines( + Contains("HEAD detached"), + Contains("branch1"), + Contains("branch2"), + Contains("master"), + ) + }, +}) diff --git a/pkg/integration/tests/branch/squash_merge_during_rebase.go b/pkg/integration/tests/branch/squash_merge_during_rebase.go new file mode 100644 index 000000000..9d8f1babc --- /dev/null +++ b/pkg/integration/tests/branch/squash_merge_during_rebase.go @@ -0,0 +1,114 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var SquashMergeDuringRebase = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Squash merge a branch with conflicts at the base of an interactive rebase, then continue the merge and the rebase", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Git.LocalBranchSortOrder = "alphabetical" + }, + SetupRepo: func(shell *Shell) { + shell. + CreateFileAndAdd("file", "original\n"). + Commit("original"). + NewBranch("other"). + UpdateFileAndAdd("file", "other change\n"). + Commit("other change"). + Checkout("master"). + UpdateFileAndAdd("file", "master change\n"). + Commit("master change"). + NewBranch("mybranch"). + CreateFileAndAdd("file2", "content\n"). + Commit("mybranch 1"). + CreateFileAndAdd("file3", "content\n"). + Commit("mybranch 2") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("mybranch 2").IsSelected(), + Contains("mybranch 1"), + Contains("master change"), + Contains("original"), + ). + Press(keys.Commits.StartInteractiveRebase). + Lines( + Contains("─── Pending rebase todos"), + Contains("mybranch 2").IsSelected(), + Contains("mybranch 1"), + Contains("─── Commits"), + Contains("master change"), + Contains("original"), + ) + + t.Views().Branches(). + Focus(). + Lines( + Contains("(no branch, rebasing mybranch)"), + Contains("master"), + Contains("mybranch").IsSelected(), + Contains("other"), + ). + NavigateToLine(Contains("other")). + Press(keys.Branches.MergeIntoCurrentBranch) + + t.ExpectPopup().Menu(). + Title(Equals("Merge")). + Select(Contains("Squash merge and commit")). + Tooltip(Equals("Squash merge 'other' into 'mybranch' as a single commit.")). + Confirm() + + t.Common().AcknowledgeConflicts() + + t.Views().Status().Content(Contains("(merging) repo → ")) + + t.GlobalPress(keys.Universal.CreateRebaseOptionsMenu) + t.ExpectPopup().Menu(). + Title(Equals("Merge options")). + Cancel() + + t.Views().Files(). + IsFocused(). + Lines( + Contains("UU file").IsSelected(), + ). + Tap(func() { + t.Shell().UpdateFile("file", "resolved\n") + }). + Press(keys.Universal.Refresh) + + t.Common().ContinueOnConflictsResolved("merge") + + t.Views().Status().Content(Contains("(rebasing) repo → ")) + + t.Views().Commits(). + Lines( + Contains("─── Pending rebase todos"), + Contains("mybranch 2"), + Contains("mybranch 1"), + Contains("─── Commits"), + Contains("Squash merge other into mybranch"), + Contains("master change"), + Contains("original"), + ) + + t.Common().ContinueRebase() + + t.Views().Status().Content(Equals("repo → mybranch")) + + t.Views().Commits(). + Lines( + Contains("mybranch 2"), + Contains("mybranch 1"), + Contains("Squash merge other into mybranch"), + Contains("master change"), + Contains("original"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index c4680bbcc..550d6bae7 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -71,6 +71,7 @@ var tests = []*components.IntegrationTest{ branch.DeleteWhileFiltering, branch.DetachedHead, branch.MergeFastForward, + branch.MergeIntoDetachedHead, branch.MergeNonFastForward, branch.MoveCommitsToNewBranchFromBaseBranch, branch.MoveCommitsToNewBranchFromMainBranch, @@ -113,6 +114,7 @@ var tests = []*components.IntegrationTest{ branch.SquashMerge, branch.SquashMergeAbortAfterConflicts, branch.SquashMergeContinueAfterConflicts, + branch.SquashMergeDuringRebase, branch.SquashMergeUncommittedWithConflicts, branch.SquashMergeWithStagedChanges, branch.Suggestions, diff --git a/schema-master/config.json b/schema-master/config.json index 869b8fb60..f956217ca 100644 --- a/schema-master/config.json +++ b/schema-master/config.json @@ -3988,7 +3988,7 @@ }, "squashMergeMessage": { "type": "string", - "description": "The commit message to use for a squash merge commit. Can contain \"{{selectedRef}}\" and \"{{currentBranch}}\" placeholders.", + "description": "The commit message to use for a squash merge commit. Can contain \"{{selectedRef}}\" and \"{{currentBranch}}\" placeholders.\nIf HEAD is detached, \"{{currentBranch}}\" is the branch being rebased, or \"HEAD\" if no rebase is in progress.", "default": "Squash merge {{selectedRef}} into {{currentBranch}}" } },