Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I push to one url and pull from another using one remote?

Tags:

git

github

I've create a clone of this repository. If I run the following command, I see that my local repo is configured to use my clone to fetch/pull, as expected.

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/domurtag/airbrake-grails.git
  Push  URL: https://github.com/domurtag/airbrake-grails.git

I would like my local repo to instead fetch from the master repo (the one I cloned from) and push to my clone. The first thing I did was add the master repo as a remote:

$ git remote add cavneb https://github.com/cavneb/airbrake-grails.git

If I again run git remote show origin I see the same output, so obviously I need to do something else to indicate that the cavneb repo should be used for fetching, but I'm not sure what this is.

In case it's relevant, I've shown the contents of my .git/config below:

[core]
    repositoryformatversion = 0
    filemode = true
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/domurtag/airbrake-grails.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[remote "cavneb"]
    url = https://github.com/cavneb/airbrake-grails.git
    fetch = +refs/heads/*:refs/remotes/cavneb/*
like image 906
Dónal Avatar asked Apr 09 '13 13:04

Dónal


3 Answers

Straight Up Answer

The command you are looking for is:

git remote set-url origin https://github.com/cavneb/airbrake-grails.git
git remote set-url origin --push https://github.com/domurtag/airbrake-grails.git

HOWEVER

But I would strongly advice against doing that.

Even though I'm not totally sure, with the above behaviour, git would/could get confused a lot.

When done like above, git assumes that this is the same repo. So when you push, the result of the next fetch should change – which it obviously won’t.

Best Practice

The proper way to handle your situation is to add the master repo as an extra remote. Again, as another best practice, I would call this repo upstream.

And when you want to pull from that extra repo(upstream):

git fetch upstream
git merge upstream/master
like image 152
Chronial Avatar answered Nov 15 '22 08:11

Chronial


Git 1.8.3 introduces settings that help with triangular workflows.

remote.pushdefault: the remote to push changes to. Can be overriden on a specific branch by setting branch.<branch>.pushremote.

With either setting, git push (with no further arguments) will push to your preferred remote, and not the branch's upstream remote. See the 1.8.3-rc2 announcement for details.

like image 32
Tobu Avatar answered Nov 15 '22 09:11

Tobu


Unless i'm mistaken, there is no such thing as a --pull option for git remote set-url : https://git-scm.com/docs/git-remote#git-remote-emset-urlem

like image 20
Arkhena Avatar answered Nov 15 '22 08:11

Arkhena