Search code examples
gitgit-branchgit-remote

How to set a git branch to push to a remote with a different branch name and pull from completely different url


My local git repo needs to pull from one server. It then needs to push a specific branch to a review repo with a different branch name on a different server.

Something like: Pull everything from PullOnlyRepo on Server1 (we'll call that origin maybe?) Push Branch hotfix to ReivewRepo with branch name JistChanges on Server2.

Right now git config -l shows:

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=<URL for Server1>
remote.origin.pushurl=no_push (this shouldn't matter since it is a pull only repo)
branch.production.remote=origin
branch.production.merge=refs/heads/production
remote.review.url=<URL for Server2>
remote.review.fetch=+refs/heads/*:refs/remotes/review/*

git pull does what I want (fetch changes from the correct place on Server1 and merges them into my work tree).

However git push doesn't. In order to achieve what I want I have to do

git push review hotfix:JistChanges

Is there some way to make git pull do this without having to put in the extra stuff?

There are some questions out there already that set up so that your local branch pushes to a remote with a different branch name. However they also change the upstream and where the pull comes from.


Solution

  • You could set the upstream branch for hotfix:

    git config push.default upstream
    git config push.default review 
    git branch --set-upstream hotfix review/JistChanges
    

    See "How do you make an existing git branch track a remote branch?" See "What is the result of git push origin?" on the default push policy (soon to be "simple")

    Starting git1.8.0:

    git branch -u review/JistChanges hotfix
    

    The OP jistanidiot reports having achieved the same result with:

    git remote set-url --push origin
    git config remote.origin.push refs/heads/hotfix:JistChanges