9

From what I remember, if I am working on a branch, e.g., my-feature-branch, I used to be able to do this from the magit-status menu with P P, and then selecting the remote branch from a list which had origin/my-feature-branch at the top. This worked beautifully.

Recently, as of this discussion, I could do something similar with P -u e (opening the magit-push-popup, setting the --set-upstream flag, and using e for elsewhere), which would similarly let me pick from a list of branches starting with origin/my-feature-branch. This worked fine for me too. Even more recently however, this no longer works, and I haven't been able to figure out the best way to set an upstream branch.

When I use P e the first option for a branch to push to is origin/my-feature-branch, which is almost always what I want. However, it doesn't set origin/my-feature-branch as the upstream for my local branch. Using P u allows me to pick the upstream branch, but origin/my-feature-branch is not in the list, and I have to type out origin/my-feature-branch without tab completion (I know this is not hard, but it is prone to user error, and is slower than the command line).

Is this the only way to set an upstream branch now, or am I missing something? I have looked at other discussions and documentation related to the issue, but all the sources I have found seem to be out of date.

  • 1
    I think the <remote>/<branch> part was accidentally left out of the prompt (see this comment). In the meantime, you can set magit-push-current-set-remote-if-missing to nil. – Kyle Meyer Dec 29 '15 at 19:27
  • I've just fixed that. My answer below assumes that fix. – tarsius Dec 29 '15 at 20:19
  • Found and fixed some other related issues. You'll probably have to wait a few hours for Melpa to catch up before everything below is 100% correct. – tarsius Dec 29 '15 at 21:23
8

There are several ways to set the upstream, while pushing or without pushing at the same time.

If magit-push-current-set-remote-if-missing is non-nil (the default), then you will see something like:

Push feature to
 p pushRemote, after setting that
 u @{upstream}, after setting that
 e elsewhere

To push to origin/feature and set that as upstream press u and then RET to accept the default completion candidate.

If magit-push-current-set-remote-if-missing is nil, then you would see this instead:

Push feature to
 e elsewhere

If you did press p now, then you would be told that the upstream is not configured (implying that you cannot push to the upstream if there is no upstream).

But if magit-push-current-set-remote-if-missing is nil, then the list of switches would feature --set-upstream, so you could use P - u p as you are used to (except that the second p is lowercase). There's one complication though: after changing the value of magit-push-current-set-remote-if-missing you have to restart Emacs for --set-upstream to appear or disappear.

Finally you can set the upstream using the "set upstream (and do nothing else)" command: b uorigin/masterRET. Note that when you use this approach, then you can only select a branch which already exists.


But it would be better to configure the push-remote and push to that. To learn more about the push-remote and how it differs from the upstream, see the Branching node in the info manual (the web version hasn't been updated yet).

Basically the upstream branch is the branch into which your feature branch will eventually be merged (by merging or rebasing, not by pushing), most likely origin/master. And the push-remote is where you push your feature branches to while still working on them or so that someone else can merge them. If the local branch is named feature and the push-remote is my-fork, then pushing that branch using P p would push to my-fork/feature. (The "push-to-branch" cannot be configured, the name of the branch on the push-remote is always the same as the local name.)

So while your question was something like "how do I push to the upstream, while configuring the upstream at the same time", my recommendation is to not push to the upstream at all, but to instead push to the push-remote.

Provided you have not changed the value of magit-push-current-set-remote-if-missing you can configure the push-remote using P psome-remoteRET. But, since you likely push all feature branches to the same remote, it is better to set the push-remote once for all branches and be done with it: b and then M-p until the right remote is selected.


Also note that it should usually not be necessary to explicitly set the upstream branch. When you create a new branch and select a remote branch as starting-point, then that is used as the upstream.

Unfortunately the starting-point usually is a local branch and in that case Git by default doesn't use it as the upstream. But that can easily be fixed by running this once:

git config --global branch.autoSetupMerge always

The default value is true, which means "set starting-point as upstream, provided it is a remote branch".


By the way, the same applies to the push-remote. That too should usually be set semi-automatically in "new" repositories. If you clone a repository, you will be asked whether you want to use origin as the push-remote. You should answer "yes", unless you are going to add another remote, say my-fork, which should be used as the push-remote. When you add a new remote using M a and remote.pushDefault isn't set yet, then you will be asked whether you want to use the newly added remote as the push-remote.

This can be configured using magit-clone-set-remote.pushDefault and magit-remote-add-set-remote.pushDefault.

Another advanced option is magit-branch-prefer-remote-upstream, which defaults to nil. If you set it to t, and then select a local branch as the starting-point for a new branch, then the upstream of the starting-point might (according to some rules, see doc-string) be used as the upstream, instead of the starting-point itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.