简体   繁体   English

如何使用一个遥控器推送到一个网址并从另一个网址拉取?

[英]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.如果我再次运行git remote show origin我会看到相同的输出,所以显然我需要做一些其他的事情来表明应该使用cavneb repo 来获取,但我不确定这是什么。

In case it's relevant, I've shown the contents of my .git/config below:如果相关,我已经在下面显示了我的.git/config的内容:

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

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.即使我不完全确定,对于上述行为, git会/可能会感到困惑。

When done like above, git assumes that this is the same repo.像上面那样完成后, git假定这是同一个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.处理您的情况的正确方法是将master库添加为额外的远程存储库。 Again, as another best practice, I would call this repo upstream .同样,作为另一个最佳实践,我会将此 repo 称为upstream

And when you want to pull from that extra repo( upstream ):当你想从那个额外的 repo( upstream ) 中提取时:

git fetch upstream
git merge upstream/master

Git 1.8.3 introduces settings that help with triangular workflows. Git 1.8.3引入了有助于三角工作流的设置。

remote.pushdefault : the remote to push changes to. remote.pushdefault :要将更改推remote.pushdefault的遥控器。 Can be overriden on a specific branch by setting branch.<branch>.pushremote .可以通过设置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.无论使用哪种设置, git push (没有进一步的参数)将推送到您首选的远程,而不是分支的上游远程。 See the 1.8.3-rc2 announcement for details.有关详细信息,请参阅1.8.3-rc2 公告

除非我弄错了,否则git remote set-url没有--pull选项之类的东西: https : --pull

Try using the set-url with pull and push:尝试使用带有拉和推的 set-url:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM