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 <noreply@anthropic.com>
This commit is contained in:
Stefan HallerandClaude Opus 5.5 committed 2026-11-12 15:26:39 +01:00
1 parent af3cad16eb
commit b57061ebeb
3 files changed
+56

No files matched your search

+7
View File
@@ -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 `<c-j>`
and `<c-k>`. 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
@@ -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)
}
@@ -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"),
)
},
})