Dónal
Dónal

Reputation: 187529

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

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/*

Upvotes: 6

Views: 2116

Answers (4)

Chronial
Chronial

Reputation: 70683

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

Upvotes: 12

Arkhena
Arkhena

Reputation: 280

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

Upvotes: 1

Tobu
Tobu

Reputation: 25426

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.

Upvotes: 4

bubba
bubba

Reputation: 3847

Try using the set-url with pull and push:

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

Upvotes: 0

Related Questions