From b57061ebebbfd0a2fa0d127b2a6980cbbc5dfc1a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 6 Nov 2026 10:16:13 +0100 Subject: [PATCH] Stop at branch boundaries when moving commits up or down To add a commit at the end of a lower branch of a stack, the obvious way is to create it at the top of the stack and move it down. Outside of a rebase, pressing ctrl+j doesn't get it there. Once the commit is at the bottom of the second branch, the next ctrl+j moves it past the update-ref todo of the first branch and past that branch's last commit. We made it work like this deliberately in #4096. Moving the commit past the update-ref todo only would have required keeping the selection where it is, and this was hard to do. Now that the destination of a move is named explicitly, it is easy. So if the commit below the selected commits is the head of branches of the stack, ctrl+j now moves them past the update-ref todos of all these branches at once, and keeps them selected. The branch marker moves up to the bottom selected commit. The next ctrl+j moves them further into the lower branch. ctrl+k does the same the other way round: if the topmost selected commit is the head of branches, it moves the commits past their update-ref todos first, to the start of the branch above. Co-Authored-By: Claude Opus 5.5 --- docs-master/Stacked_Branches.md | 7 ++++ .../controllers/local_commits_controller.go | 10 +++++ ...e_across_branch_boundary_outside_rebase.go | 39 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/docs-master/Stacked_Branches.md b/docs-master/Stacked_Branches.md index 6bbd7ae56..8f0624749 100644 --- a/docs-master/Stacked_Branches.md +++ b/docs-master/Stacked_Branches.md @@ -19,6 +19,13 @@ fonts](Config.md#display-nerd-fonts-icons)). In the commits panel, `>` selects the first commit of the branch that the selection is in. Press it again to get to the first commit of the next branch down the stack. +To move commits from one branch of the stack to another, move them with `` +and ``. When they get to the boundary between two branches, the next press +moves them across the boundary only. The order of the commits stays the same; +only the branch marker moves. So to add a commit at the end of a branch further +down in the stack, you can create it at the top of the stack and move it down +until the marker of that branch is on it. + When you push the topmost branch of the stack with `P`, and the branches below it have commits that haven't been pushed yet, lazygit offers to push them along with it. After rebasing the stack this saves you from checking out and diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 579fda30f..e90bd02fc 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -1268,12 +1268,22 @@ func moveCommitsToArgs( func (self *LocalCommitsController) moveDown(selectedCommits []*models.Commit, startIdx int, endIdx int) error { updateRefBranches := self.context().UpdateRefBranches() destination := commitsDestination{index: endIdx + 2} + if len(updateRefBranchesAt(updateRefBranches, endIdx+1)) > 0 { + // The commit below is the head of branches of a stack. Move to the end + // of these branches, past their update-ref todos only. + destination = destinationBelowUpdateRefs(updateRefBranches, endIdx+1) + } return self.move(selectedCommits, startIdx, endIdx, destination, updateRefBranches, nil) } func (self *LocalCommitsController) moveUp(selectedCommits []*models.Commit, startIdx int, endIdx int) error { updateRefBranches := self.context().UpdateRefBranches() destination := destinationBelowUpdateRefs(updateRefBranches, startIdx-1) + if len(updateRefBranchesAt(updateRefBranches, startIdx)) > 0 { + // The topmost commit is the head of branches of a stack. Move to the + // beginning of the branch above, past their update-ref todos only. + destination = commitsDestination{index: startIdx} + } return self.move(selectedCommits, startIdx, endIdx, destination, updateRefBranches, nil) } diff --git a/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go b/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go index 36f1f5312..a75ec54bc 100644 --- a/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go +++ b/pkg/integration/tests/interactive_rebase/move_across_branch_boundary_outside_rebase.go @@ -19,6 +19,9 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes CreateNCommits(1). NewBranch("branch1"). CreateNCommitsStartingAt(2, 2). + // A second branch at the head of branch1, so that the boundary + // consists of two update-ref todos + NewBranch("branch1b"). NewBranch("branch2"). CreateNCommitsStartingAt(2, 4) @@ -35,6 +38,16 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes Contains("CI commit-01"), ). NavigateToLine(Contains("commit-04")). + // Moving the commit down past both update-ref todos makes it the + // head of both branches; it stays selected + Press(keys.Commits.MoveDownCommit). + Lines( + Contains("CI commit-05"), + Contains("CI * commit-04").IsSelected(), + Contains("CI commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). Press(keys.Commits.MoveDownCommit). Lines( Contains("CI commit-05"), @@ -42,6 +55,32 @@ var MoveAcrossBranchBoundaryOutsideRebase = NewIntegrationTest(NewIntegrationTes Contains("CI commit-04").IsSelected(), Contains("CI commit-02"), Contains("CI commit-01"), + ). + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-05"), + Contains("CI * commit-04").IsSelected(), + Contains("CI commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). + // Moving it up past the update-ref todos makes it the first commit + // of branch2 again + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-05"), + Contains("CI commit-04").IsSelected(), + Contains("CI * commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), + ). + Press(keys.Commits.MoveUpCommit). + Lines( + Contains("CI commit-04").IsSelected(), + Contains("CI commit-05"), + Contains("CI * commit-03"), + Contains("CI commit-02"), + Contains("CI commit-01"), ) }, })