Alexander Lomia Alexander Lomia - 2 months ago 7
Git Question

How to push by default to a differently named branch?

I have two branches:

master
demo_master


And two remotes:

origin
demo_origin


How can I configure git to automatically push master to origin/master and demo_master to demo_origin/master when issuing
git push
command?




I've tried to achieve the desired result by

git push --set-upstream demo_origin demo_master:master


However, git spits out the following error whenever I try to use
git push
:


fatal: The upstream branch of your current branch does not match the
name of your current branch. To push to the upstream branch on the
remote, use

git push demo_origin HEAD:master


To push to the branch of the same name on the remote, use

git push demo_origin demo_master


Answer

You need to set the push.default configuration properly:

Defines the action git push should take if no refspec is explicitly given. Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want.

With Git 2.0, this setting was set to simple by default, to make it easy for beginners:

simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners.

As the option says, Git will refuse to push to a branch that has a different name. In order to achieve that, you need to set it to upstream:

upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

So, do git config -g push.default upstream, and then it should work just fine with git push --set-upstream demo_origin master:demo_master. (note that the local branch comes first in local:remote)

Comments