2

I have a branch which pulls from one remote and pushes to another and U used git branch --set-upstream-to=xxxx xxxx to set the pull repo and git config remote.origin.pushurl user@user.com:repo.git to set the push repo.

Although the pull is from the master branch on the source repo, the push goes to the upstream branch on the destination repo.

When I switch to the branch and I do a git push I get the usual --global push.default message:

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Is there a way to specify for a particular branch which branch of the remote it should push to, rather than the push.default value, and similarly for pulls as well?

vfclists
  • 19,193
  • 21
  • 73
  • 92

1 Answers1

1

the idea is to push to any arbitrarily named branch that is different from the branch name

Simply specify an upstream branch:

git branch --set-upstream-to my_local_branch origin/my_remote_branch

Then subsequent pushes will know to which branch to push my_local_branch.

You would still need to setup a push policy (git config push.default simple)

To push to one repo, but pull from another, you can setup the pushurl to be different from the pull/fetch url

git config remote.origin.pushurl /url/for/origin/repo
git config remote.origin.url /url/for/upstream/repo

That would allow to manage everything with "one" remote (which actually references two different repos)

You can also update the refspecs for the upstream branch part:

git config remote.origin.push refs/heads/my_local_branch:refs/heads/my_remote_branch
git config remote.origin.fetch refs/heads/my_local_branch:refs/heads/my_local_branch
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I haven't really played with this yet, just reading the manpages. You still have to provide the local branch name for either `matching` or `simple`, don't you (meaning a ref-less `git push` will not suffice)? – musiKk Mar 18 '15 at 08:14
  • The repo that is pulled from is not the same repo that is pushed to. `--set-upstream-to` specifies the branch pulled from, and the branch names are different. The pull is from `master` on the source repo, but `upstream` on the destination repo. The `upstream` repo is never pushed to. I wanted to edit the comment but it timed out – vfclists Mar 18 '15 at 08:15
  • Your latest answer with the `remote.origin.pushurl` setting is what I was inquiring about. Does that configuration syntax allow one to specify the branch as well, ie `git config remote.origin.pushurl /url/for/origin/repo/`, in order for `git push` to work even if the `--global push.default` has not been set? – vfclists Mar 18 '15 at 08:31
  • @vfclists the branch is set by the push refspec, not the push url. See http://git-scm.com/docs/git-push#REMOTES, and http://longair.net/blog/2011/02/27/an-asymmetry-between-git-pull-and-git-push/: “refspecs” are usually of the form `:`, telling you which local branch (src) you’d like to update the remote branch (def) with. However, the default behaviour if you don’t add `:`, as in this example, is explained as If `:` is omitted, the same ref as `` will be updated. – VonC Mar 18 '15 at 08:40
  • In my instance the branch pulls from master branch in the source repo and pushes to the upstream branch in the destination. Would that result in some like this: ` [remote "upstream"]` ` url = https://github.com/sourceuser/repo.git` ` fetch = +refs/heads/*:refs/remotes/upstream/*` ` tagopt = --tags` ` pushurl = https://github.com/destuser/repo.git` ` push = +refs/heads/upstream:refs/remotes/upstream` – vfclists Mar 18 '15 at 09:47
  • @vfclists yes, my idea was to associate those different urls to "`origin`" in order to have a default `git fetch` or `git push` to do the right thing. – VonC Mar 18 '15 at 09:54
  • Can you update the answer with the refspec example? I thought the issue could be resolved from the command line, but in this case it appears to require editing of `.git/config` – vfclists Mar 18 '15 at 10:17
  • @vfclists I have updated the answer (not tested) with a git config similar to http://stackoverflow.com/a/1915046/6309 – VonC Mar 18 '15 at 10:33