2

Pushing from local branch "master2" to remote "origin2" creates a new remote branch "origin2/master2". How can I have it push automatically to "origin2/master"?

I have two remote tracking branches known as

origin/master
origin2/master

I have two local branches:

master
master2

Created as follows:

git checkout -b master origin/master
git checkout -b master2 origin2/master

Git tells me that it has automatically set up the local branches to track the remote branches; example:

Branch master2 set up to track remote branch master from origin2.

I've had no problem with local branch master pulling and pushing to origin/master by simply using "git pull" and "git push" while on that branch. However, when I'm on master2, "git push" creates a remote branch:

origin2/master2

My config has the following entries generated automatically:

branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.master2.remote=origin2
branch.master2.merge=refs/heads/master

I'm aware that I could do this, but I'm wondering if there's a config setting to get around it:

git push origin2 master2:master

Output of git --version:

git version 1.8.2.1

Note: I've obfuscated some names here, so hopefully I didn't make any mistakes in the transpose. Please address them in the comments if you see any and I'll fix them.

Bryant
  • 334
  • 1
  • 11
  • check push.default, http://stackoverflow.com/questions/21839651/git-what-is-the-difference-between-push-default-matching-and-simple – Andrew C Feb 04 '16 at 20:40
  • See [this answer](http://stackoverflow.com/a/21866819/1256452) from question that Andrew C linked. Specifically you probably want to use the `upstream` setting. Note that you can set this per repository instead of globally (it's your choice how to fuss with `push.default`, you can even set this globally *and* per-repository). – torek Feb 04 '16 at 20:47
  • The question wasn't terribly helpful, but you're right. The answer was to set push.default=upstream. – Bryant Feb 04 '16 at 20:50

2 Answers2

2

You can run git push --set-upstream origin2 master2:master if you're pushing the branch for the first time. The branch will be set to track origin2/master.

You can also set the upstream branch of an existing branch: git checkout master2; git branch -u origin2/master. This also sets the tracking branch to origin2/master, any subsequent git push will push to origin2/master.

See git's documentation about tracking branches for details.

morxa
  • 3,221
  • 3
  • 27
  • 43
  • The refspec style name:name stuff applies only for fetch and push, not for upstream settings. But you're right that the general method here is to fuss with the push refspec. – torek Feb 04 '16 at 20:45
  • I posted my config settings and mentioned that the upstream was already set, but thanks. – Bryant Feb 04 '16 at 20:54
2

The solution is to set:

push.default=upstream
Bryant
  • 334
  • 1
  • 11