gitgit-pushgit-pullgit-refspec

Does a Git a push configuration allow specifying the branch as well?


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?


Solution

  • 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