57

When you push a branch with Magit (with P P) and the branch has no upstream, Magit asks you the name of the branch you want to push to. That's great.

Once you set the upstream for a branch, however, Magit no longer asks you where you want to push it. This is usually convenient (you don't want to be asked every time), but it makes it difficult to push a branch to a different remote branch.

Is there a way to have Magit ask me again about the destination branch, even if the current branch already has an upstream?


Use-case

It's quite common that I have a branch like this:

Local:    issue-30 ~/Git-Projects/repo/
Remote:   issue-30 @ origin (git@github.com:.../repo.git)
Head:     ebe4054 Some commit message

and I want to change both Local and Remote branches to issue-30-and-34. If I just rename the Local branch, I'm left with this:

Local:    issue-30-and-34 ~/Git-Projects/repo/
Remote:   issue-30 @ origin (git@github.com:.../repo.git)
Head:     ebe4054 Some commit message

Even if I delete origin/issue-30, doing P P on issue-30-and-34 will simply recreate the origin/issue-30.

What I end up doing is the following, but it feels rather roundabout for something that can be done in a single git command:

  1. Create a new branch called issue-30-and-34 (instead of renaming the old one),
  2. delete both Local and Remote versions of issue-30,
  3. push issue-30-and-34 (since it has no upstream yet, Magit will ask me where to push).
Malabarba
  • 22,878
  • 6
  • 78
  • 163

5 Answers5

80

Starting with v2.1 you can change the upstream of the current branch to a remote or local branch using bu[REMOTE/]BRANCHRET. Starting with v2.4, if an upstream is already set, then bu will just unset it. Press u again to set a new value.

Starting with v2.4 and if the upstream is unset, you can also set upstream while pushing by using pu[REMOTE/]BRANCHRET. You'll automatically be asked what branch to use as the upstream. Once an upstream has been set pu will push to that without letting you change the upstream. So to change the upstream, you have to use the approach described in the previous paragraph.

Before v2.4 it was also possible to set the remote branch that you are pushing to as the upstream branch using p-ueREMOTE/BRANCHRET. But now the --set-upstream switch is no longer available in the push popup by default. You can get it back by setting magit-push-current-set-remote-if-missing to nil. That has two effects: the --set-upstream switch will be available again after the next restart and the approach described in the previous paragraph will no longer work.


If you want both features described in the second and third paragraph to be available at the same time, then leave magit-push-current-set-remote-if-missing untouched and add this to you init file:

(magit-define-popup-switch 'magit-push-popup
  ?u "Set upstream" "--set-upstream")
tarsius
  • 25,298
  • 4
  • 69
  • 109
11

Press "P C-u C-u P" to make Magit ask you what remote and remote branch to use before pushing.

(With a single prefix argument ("P C-u P") Magit asks what remote to use.)

I found this by pressing P C-h k P in a Magit status buffer, reading the docs of magit-push and magit-push-dwim (these docs seem to be out of date, BTW), and experimenting.

PS: I use Magit magit-1.2.0-2049-ge6839e8.

Constantine
  • 9,072
  • 1
  • 34
  • 49
2

It looks like it's under the branch/configure section now:

b C p to set the pushRemote

1

As of Nov. 2016, and the latest magit, here's a partial solution:

P e - allows you to push elsewhere, but it's not remembered

I couldn't find a way for magit to --set-upstream, so I just did this from terminal, kind of a cheat but it got me over this problem:

git push --set-upstream origin <branch-name>

And now the new upstream is saved, and I can build my next thing :)

Josh.F
  • 111
  • 3
  • 1
    "I can't find a way[...]to `--set-upstream`" - Perhaps you should try reading [tarsius' answer](http://emacs.stackexchange.com/a/6023/5296) – npostavs Nov 17 '16 at 02:01
  • sure, it's what helped me figure this out. Just, `magit` has changed a little since then, and my hope is that my solution saves someone an extra few minutes – Josh.F Nov 17 '16 at 15:06
  • 1
    The first paragraph of that answer explains how to change the upstream, and it works in the latest magit. – npostavs Nov 17 '16 at 15:25
0

Use P C P to set the pushRemote for every branch on a checked out repo using magit, using the Push -> Configure menu. It will probably automatically select origin for you.

hraban
  • 211
  • 1
  • 9