1

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 
2

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

That has been rewritten with Git 2.33 (Q3 2021), adding clarification around git push.

See commit 90cfb26, commit 7e6d72b, commit b8e8b98, commit 6b010c8, commit d099b9c, commit 3b9fd83, commit 050f76b (31 May 2021) by Felipe Contreras (felipec).
(Merged by Junio C Hamano -- gitster -- in commit 07e230d, 13 Jul 2021)

doc: push: explain default=simple correctly

Cc: Elijah Newren
Signed-off-by: Felipe Contreras

The simple mode only barfs when working on a centralized workflow, and there's no configured upstream branch with the same name.

git config now includes in its man page:

simple

Pushes the current branch with the same name on the remote.

If you are working on a centralized workflow (pushing to the same repository you pull from, which is typically origin), then you need to configure an upstream branch with the same name.

This mode is the default since Git 2.0, and is the safest option suited for beginners.


That explains why the default setting is not the right one for your case, and you need, as noted above:

git config -g push.default upstream, git push --set-upstream demo_origin master:demo_master 

Note the second push is to be done only for the first push: once the upstream branch is set, a simple git push (no parameters) will be enough.

Comments

0

On master local branch use git push origin -u master and on the demo_master branch use git push demo_origin -u master

Another solution would be using git branch -u origin/master from master branch and git branch -u demo_origin/master from demo_master branch.

https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches

2 Comments

I want to push demo_master to demo_origin master. Aren't these commands equivalent to what I was doing?
I think that the right way to achieve it using set-upstream-to is git branch --set-upstream-to demo_origin/master when you are on the demo_master branch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.