Skip to content

Commit cccd5f9

Browse files
committed
When creating a new branch off of a main branch that is behind its upstream, use the upstream instead
Very often my local main branch is behind its upstream, because I have no reason to keep it up to date. In the olden days it used to be necessary to keep it up to date in order to rebase feature branches onto it, so you'd have to press `f` on it occasionally before pressing `r`. This is no longer necessary now that we have the "rebase onto base branch" command (`r b`). The only time I now need an up-to-date local main is when I want to create a new branch off of it. This is annoying, because I have to press `f` and wait for it to complete before I can create a new branch; but I know that origin/main is probably up to date (or close to up to date), because lazygit fetches it in the background all the time. So why can't I just create my new branch off of origin/main, then? (Sure, I could switch over to the Remotes tab, open origin, and select origin/main there, but that's cumbersome.) So this is exactly what this commit does: if the local main is strictly behind its upstream (as opposed to diverged, which should be very rare), then when pressing `n` on it we create the new branch off of origin/main.
1 parent 5f80980 commit cccd5f9

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

pkg/gui/controllers/branches_controller.go

+9
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,15 @@ func (self *BranchesController) rename(branch *models.Branch) error {
731731
}
732732

733733
func (self *BranchesController) newBranch(selectedBranch *models.Branch) error {
734+
isMainBranch := lo.Contains(self.c.UserConfig().Git.MainBranches, selectedBranch.Name)
735+
if isMainBranch && selectedBranch.RemoteBranchStoredLocally() {
736+
isStrictlyBehind := selectedBranch.IsBehindForPull() && !selectedBranch.IsAheadForPull()
737+
if isStrictlyBehind {
738+
return self.c.Helpers().Refs.NewBranch(
739+
selectedBranch.FullUpstreamRefName(), selectedBranch.ShortUpstreamRefName(), "")
740+
}
741+
}
742+
734743
return self.c.Helpers().Refs.NewBranch(selectedBranch.FullRefName(), selectedBranch.RefName(), "")
735744
}
736745

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package branch
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var NewBranchFromMainBranchBehindUpstream = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Create a new branch from a main branch that is behind its upstream",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.CreateNCommits(2)
15+
shell.CloneIntoRemote("origin")
16+
shell.PushBranchAndSetUpstream("origin", "master")
17+
shell.HardReset("HEAD^")
18+
},
19+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
20+
t.Views().Branches().
21+
Focus().
22+
Lines(
23+
Contains("master ↓1").DoesNotContain("↑").IsSelected(),
24+
).
25+
Press(keys.Universal.New)
26+
27+
t.ExpectPopup().Prompt().
28+
Title(Contains("New branch name (branch is off of 'origin/master')")).
29+
Type("new-branch").
30+
Confirm()
31+
32+
t.Views().Branches().
33+
Lines(
34+
Contains("new-branch").IsSelected(),
35+
Contains("master"),
36+
)
37+
38+
t.Views().Commits().
39+
Lines(
40+
Contains("commit 02"),
41+
Contains("commit 01"),
42+
)
43+
},
44+
})

pkg/integration/tests/test_list.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var tests = []*components.IntegrationTest{
4848
branch.DeleteWhileFiltering,
4949
branch.DetachedHead,
5050
branch.NewBranchAutostash,
51+
branch.NewBranchFromMainBranchBehindUpstream,
5152
branch.NewBranchFromRemoteTrackingDifferentName,
5253
branch.NewBranchFromRemoteTrackingSameName,
5354
branch.NewBranchWithPrefix,

0 commit comments

Comments
 (0)