jesseduffield / lazygit

simple terminal UI for git commands
MIT License
48.05k stars 1.73k forks source link

Allow for creation of remote branch whose name does not match local branch #3618

Closed socketbox closed 1 month ago

socketbox commented 1 month ago

Is your feature request related to a problem? Please describe. Let's say that I've created a local branch and committed to it. When I go to push to the remote for the first time, I have a change of mind and want to give the remote branch (perhaps the work has changed in scope) a different name.

When I do that now, I receive the following error:

Error
error: src refspec <desired remote branch name> does not match any
error: failed to push some refs to <remote .git URI>

Describe the solution you'd like I'd like to be presented with the option of creating the remote branch with the desired name.

Describe alternatives you've considered

  1. Not fulfilling this feature request but correcting the error text (which seems truncated--or perhaps this is git)
  2. Rephrasing the error text such that it's more intelligible.
  3. Telling the user to first rename the local branch and then attempt the push

Additional context image

JordanllHarper commented 1 month ago

I would be interested in this too, and might look into this. @jesseduffield would this be good as a first issue?

stefanhaller commented 1 month ago

I suspect the fix is as simple as this:

diff --git a/pkg/commands/git_commands/sync.go b/pkg/commands/git_commands/sync.go
index 4ab1f336b..d6842e655 100644
--- a/pkg/commands/git_commands/sync.go
+++ b/pkg/commands/git_commands/sync.go
@@ -33,7 +33,7 @@ func (self *SyncCommands) PushCmdObj(task gocui.Task, opts PushOpts) (oscommands
        ArgIf(opts.Force, "--force-with-lease").
        ArgIf(opts.SetUpstream, "--set-upstream").
        ArgIf(opts.UpstreamRemote != "", opts.UpstreamRemote).
-       ArgIf(opts.UpstreamBranch != "", opts.UpstreamBranch).
+       ArgIf(opts.UpstreamBranch != "", "HEAD:"+opts.UpstreamBranch).
        ToArgv()

    cmdObj := self.cmd.New(cmdArgs).PromptOnCredentialRequest(task)

Good first issue? Probably, all that's missing is adapting the failing unit tests.

There might be a few other problems related to branches whose upstream branch doesn't have the same name; for example, hitting d and choosing to delete the upstream branch doesn't work for these, as it's trying to delete an upstream branch with the same name. Another one is that pushing to such a branch fails with an error if git's push.default is set to "simple". But these could be solved independently.

JordanllHarper commented 1 month ago

Perfect! I'm very new to Go so will be happy to do this.

stefanhaller commented 1 month ago

Fixed by #3630.