From 748fe85bef623024a0d3673df6370a904e455d98 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 13:28:23 +0100 Subject: [PATCH 1/6] Fix the comment about the Name of a detached HEAD Since 5dec080719, the Name of a detached HEAD is the full commit hash, not the part of the DisplayName after "detached at". Co-Authored-By: Claude Opus 5.5 --- pkg/commands/models/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index b01e5a7ef..176c8462d 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -9,7 +9,7 @@ import ( // duplicating this for now type Branch struct { Name string - // the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name would be '123asdf' + // the displayname is something like '(HEAD detached at 123asdf)', whereas in that case the name is the full hash of the commit DisplayName string // indicator of when the branch was last checked out e.g. '2d', '3m' Recency string From 8fd0684c5692f3d013c96194a4fdbcb89323f6bd Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 13:29:22 +0100 Subject: [PATCH 2/6] Add a test showing that the branches panel has no hash or subject for a detached HEAD Co-Authored-By: Claude Opus 5.5 --- .../branch/detached_head_hash_and_subject.go | 30 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 31 insertions(+) create mode 100644 pkg/integration/tests/branch/detached_head_hash_and_subject.go diff --git a/pkg/integration/tests/branch/detached_head_hash_and_subject.go b/pkg/integration/tests/branch/detached_head_hash_and_subject.go new file mode 100644 index 000000000..e0c729b89 --- /dev/null +++ b/pkg/integration/tests/branch/detached_head_hash_and_subject.go @@ -0,0 +1,30 @@ +package branch + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var DetachedHeadHashAndSubject = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Show the commit hash and subject of a detached HEAD in the branches panel", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell. + CreateNCommits(3). + Checkout("HEAD^") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Press(keys.Universal.NextScreenMode). + Lines( + /* EXPECTED: + MatchesRegexp(`^ \* [0-9a-f]{8} \(HEAD detached at [0-9a-f]+\) {3}commit-02$`).IsSelected(), + ACTUAL: */ + MatchesRegexp(`^ \* {10}\(HEAD detached at [0-9a-f]+\) {3}$`).IsSelected(), + MatchesRegexp(`[0-9a-f]{8} master +commit-03$`), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 550d6bae7..7286c20a8 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -70,6 +70,7 @@ var tests = []*components.IntegrationTest{ branch.DeleteRemoteBranchWithDifferentName, branch.DeleteWhileFiltering, branch.DetachedHead, + branch.DetachedHeadHashAndSubject, branch.MergeFastForward, branch.MergeIntoDetachedHead, branch.MergeNonFastForward, From 9dcef62ff2e093d2e405f54ec02bbaa8fc7d7b3e Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 14:06:42 +0100 Subject: [PATCH 3/6] Show the hash and subject of a detached HEAD in the branches panel In the branches panel, the row of a detached HEAD has an empty hash column if gui.showBranchCommitHash is on. In half or full screen mode, it also has no subject. The branch loader creates this row without the hash and subject of the commit. Ask git for the subject too. Show the Name of the row in the hash column; for a detached HEAD, the Name is the full hash. Leave the row's CommitHash empty. Otherwise, code that looks for branches at a commit would take the detached HEAD for a branch. For example, the checkout menu of the HEAD commit would offer to check it out. Co-Authored-By: Claude Opus 5.5 --- pkg/commands/git_commands/branch.go | 6 ++++-- pkg/commands/git_commands/branch_loader.go | 4 +++- pkg/commands/git_commands/branch_test.go | 14 ++++++++------ pkg/commands/models/branch.go | 3 ++- pkg/gui/presentation/branches.go | 3 ++- .../tests/branch/detached_head_hash_and_subject.go | 3 --- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index 92d69c06d..d36cd20ff 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -78,7 +78,7 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { } output, err := self.cmd.New( NewGitCmd("branch"). - Arg("--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"). + Arg("--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"). ToArgv(), ).DontLog().RunWithOutput() if err != nil { @@ -86,13 +86,15 @@ func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { } for _, line := range utils.SplitLines(output) { split := strings.Split(strings.TrimRight(line, "\r\n"), "\x00") - if len(split) == 3 && split[0] == "*" { + if len(split) == 4 && split[0] == "*" { hash := split[1] displayName := split[2] + subject := split[3] return BranchInfo{ RefName: hash, DisplayName: displayName, DetachedHead: true, + Subject: subject, }, nil } } diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index 6cb61754f..0dd2178e0 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -36,6 +36,8 @@ type BranchInfo struct { RefName string DisplayName string // e.g. '(HEAD detached at 123asdf)' DetachedHead bool + // The subject of the commit that a detached HEAD points at + Subject string } // BranchLoader returns a list of Branch objects for the current repo @@ -129,7 +131,7 @@ func (self *BranchLoader) Load(reflogCommits []*models.Commit, if err != nil { return nil, err } - branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Recency: " *"}) + branches = utils.Prepend(branches, &models.Branch{Name: info.RefName, DisplayName: info.DisplayName, Head: true, DetachedHead: info.DetachedHead, Subject: info.Subject, Recency: " *"}) } configBranches := self.config.Branches(self.cmd) diff --git a/pkg/commands/git_commands/branch_test.go b/pkg/commands/git_commands/branch_test.go index 97e5ba544..ae19ce159 100644 --- a/pkg/commands/git_commands/branch_test.go +++ b/pkg/commands/git_commands/branch_test.go @@ -271,13 +271,14 @@ func TestBranchCurrentBranchInfo(t *testing.T) { "falls back to git `git branch --points-at=HEAD` if symbolic-ref fails", oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")). - ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"}, - "*\x006f71c57a8d4bd6c11399c3f55f42c815527a73a4\x00(HEAD detached at 6f71c57a)\n", nil), + ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"}, + "*\x006f71c57a8d4bd6c11399c3f55f42c815527a73a4\x00(HEAD detached at 6f71c57a)\x00Some subject\n", nil), func(info BranchInfo, err error) { assert.NoError(t, err) assert.EqualValues(t, "6f71c57a8d4bd6c11399c3f55f42c815527a73a4", info.RefName) assert.EqualValues(t, "(HEAD detached at 6f71c57a)", info.DisplayName) assert.True(t, info.DetachedHead) + assert.EqualValues(t, "Some subject", info.Subject) }, }, { @@ -285,22 +286,23 @@ func TestBranchCurrentBranchInfo(t *testing.T) { oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")). ExpectGitArgs( - []string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"}, - "*\x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00(头指针在 679b0456 分离)\n"+ - " \x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00refs/heads/master\n", + []string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"}, + "*\x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00(头指针在 679b0456 分离)\x00一些主题\n"+ + " \x00679b0456f3db7c505b398def84e7d023e5b55a8d\x00refs/heads/master\x00一些主题\n", nil), func(info BranchInfo, err error) { assert.NoError(t, err) assert.EqualValues(t, "679b0456f3db7c505b398def84e7d023e5b55a8d", info.RefName) assert.EqualValues(t, "(头指针在 679b0456 分离)", info.DisplayName) assert.True(t, info.DetachedHead) + assert.EqualValues(t, "一些主题", info.Subject) }, }, { "bubbles up error if there is one", oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"symbolic-ref", "--short", "HEAD"}, "", errors.New("error")). - ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)"}, "", errors.New("error")), + ExpectGitArgs([]string{"branch", "--points-at=HEAD", "--format=%(HEAD)%00%(objectname)%00%(refname)%00%(contents:subject)"}, "", errors.New("error")), func(info BranchInfo, err error) { assert.Error(t, err) assert.EqualValues(t, "", info.RefName) diff --git a/pkg/commands/models/branch.go b/pkg/commands/models/branch.go index 176c8462d..ce9271430 100644 --- a/pkg/commands/models/branch.go +++ b/pkg/commands/models/branch.go @@ -40,7 +40,8 @@ type Branch struct { PushBranch string // subject line in commit message Subject string - // commit hash + // commit hash; empty for a detached HEAD, whose Name is the hash. Code that + // looks for branches at a commit relies on this. CommitHash string // How far we have fallen behind our base branch. 0 means either not diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index 75abd640f..016c2f71d 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -162,7 +162,8 @@ func getBranchDisplayStrings( res = append(res, coloredPrIcon) if showCommitHash { - res = append(res, utils.ShortHash(b.CommitHash)) + // The CommitHash of a detached HEAD is empty, but its Name is the hash + res = append(res, utils.ShortHash(lo.Ternary(b.DetachedHead, b.Name, b.CommitHash))) } if divergence != "" { diff --git a/pkg/integration/tests/branch/detached_head_hash_and_subject.go b/pkg/integration/tests/branch/detached_head_hash_and_subject.go index e0c729b89..8600aae7d 100644 --- a/pkg/integration/tests/branch/detached_head_hash_and_subject.go +++ b/pkg/integration/tests/branch/detached_head_hash_and_subject.go @@ -20,10 +20,7 @@ var DetachedHeadHashAndSubject = NewIntegrationTest(NewIntegrationTestArgs{ Focus(). Press(keys.Universal.NextScreenMode). Lines( - /* EXPECTED: MatchesRegexp(`^ \* [0-9a-f]{8} \(HEAD detached at [0-9a-f]+\) {3}commit-02$`).IsSelected(), - ACTUAL: */ - MatchesRegexp(`^ \* {10}\(HEAD detached at [0-9a-f]+\) {3}$`).IsSelected(), MatchesRegexp(`[0-9a-f]{8} master +commit-03$`), ) }, From 6348e8a6af8e6760aa9593455d1985d8094860df Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 14:07:35 +0100 Subject: [PATCH 4/6] Extract CommitDescription from Commit.Description A later commit describes a detached HEAD in the same way. Make it work for hashes shorter than 7 characters. The Name of a detached HEAD is "HEAD" if `git branch --points-at=HEAD` doesn't report it. Co-Authored-By: Claude Opus 5.5 --- pkg/commands/models/commit.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/commands/models/commit.go b/pkg/commands/models/commit.go index c5b72bf55..3e4201fb6 100644 --- a/pkg/commands/models/commit.go +++ b/pkg/commands/models/commit.go @@ -151,7 +151,13 @@ func (c *Commit) ID() string { } func (c *Commit) Description() string { - return fmt.Sprintf("%s %s", c.Hash()[:7], c.Name) + return CommitDescription(c.Hash(), c.Name) +} + +// CommitDescription returns the short hash and the subject of a commit, e.g. +// "6f71c57 Fix the thing". +func CommitDescription(hash string, subject string) string { + return fmt.Sprintf("%s %s", hash[:min(7, len(hash))], subject) } func (c *Commit) IsMerge() bool { From f7c07e4fc2094393115d93daa60bccbcc26d2bf8 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 14:07:49 +0100 Subject: [PATCH 5/6] Add a test showing that the new branch prompt shows the full hash of a detached HEAD Co-Authored-By: Claude Opus 5.5 --- pkg/integration/tests/branch/detached_head.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/integration/tests/branch/detached_head.go b/pkg/integration/tests/branch/detached_head.go index 9a419540b..06f77a15c 100644 --- a/pkg/integration/tests/branch/detached_head.go +++ b/pkg/integration/tests/branch/detached_head.go @@ -25,7 +25,10 @@ var DetachedHead = NewIntegrationTest(NewIntegrationTestArgs{ Press(keys.Universal.New) t.ExpectPopup().Prompt(). - Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]+'\)$`)). + /* EXPECTED: + Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]{7} commit-09'\)$`)). + ACTUAL: */ + Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]{40}'\)$`)). Type("new-branch"). Confirm() From d2321ca27bd9d690bec8a524a6f4726c764ee200 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 14 Nov 2026 14:08:13 +0100 Subject: [PATCH 6/6] Show the short hash and subject of a detached HEAD in the new branch prompt If you press `n` on the row of a detached HEAD in the branches panel, the title of the prompt contains the full 40-character commit hash. Since 5dec080719, the Name of a detached HEAD is the full hash. Describe the commit in the same way as when you press `n` on it in the commits panel. Co-Authored-By: Claude Opus 5.5 --- pkg/gui/controllers/branches_controller.go | 7 ++++++- pkg/integration/tests/branch/detached_head.go | 3 --- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index 37ac90261..a51666ba1 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -746,7 +746,12 @@ func (self *BranchesController) rename(branch *models.Branch) error { } func (self *BranchesController) newBranch(selectedBranch *models.Branch) error { - return self.c.Helpers().Refs.NewBranch(selectedBranch.FullRefName(), selectedBranch.RefName(), "") + fromName := selectedBranch.RefName() + if selectedBranch.DetachedHead { + // The RefName of a detached HEAD is the full hash of its commit + fromName = models.CommitDescription(selectedBranch.Name, selectedBranch.Subject) + } + return self.c.Helpers().Refs.NewBranch(selectedBranch.FullRefName(), fromName, "") } func (self *BranchesController) createPullRequestMenu(selectedBranch *models.Branch, checkedOutBranch *models.Branch) error { diff --git a/pkg/integration/tests/branch/detached_head.go b/pkg/integration/tests/branch/detached_head.go index 06f77a15c..368087e75 100644 --- a/pkg/integration/tests/branch/detached_head.go +++ b/pkg/integration/tests/branch/detached_head.go @@ -25,10 +25,7 @@ var DetachedHead = NewIntegrationTest(NewIntegrationTestArgs{ Press(keys.Universal.New) t.ExpectPopup().Prompt(). - /* EXPECTED: Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]{7} commit-09'\)$`)). - ACTUAL: */ - Title(MatchesRegexp(`^New branch name \(branch is off of '[0-9a-f]{40}'\)$`)). Type("new-branch"). Confirm()