Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 469
Jistanidiot Avatar asked Oct 18 '12 20:10

Jistanidiot


People also ask

How push local branch to remote branch with different name?

In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

How do I specify which branch to push to?

To push to a branch of a different name, you just need to specify the branch you want to push and the name of the branch you want to push to separated by a colon ( : ).

How do I pull from a specific branch remote?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How do I change a branch to a remote branch?

In order to switch to a remote branch, make sure to fetch your remote branch with “git fetch” first. You can then switch to it by executing “git checkout” with the “-t” option and the name of the branch.


2 Answers

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
like image 100
VonC Avatar answered Dec 30 '22 18:12

VonC


Ok VonC's answer got me on the right track. Here's what I did:

git remote set-url --push origin <review repo URL>
git config remote.origin.push refs/heads/hotfix:JistChanges

The only problem with this is that now everything pushes to the review repo. That's ok for now since 90% of the work will be in this branch. The other 10% I can just do a git push other where other the same origin repo with the correct push configuration.

like image 22
Jistanidiot Avatar answered Dec 30 '22 19:12

Jistanidiot