简体   繁体   English

如何配置git从一个存储库中提取并推送到另一个存储库

[英]How to configure git to pull from one repository and push to another

I have two remote repositories: a main repository and a fork, and want to pull new changes from the main repo and push to my fork so I can create pull-requests to the main repo. 我有两个远程存储库:一个主存储库和一个fork,并希望从主repo中提取新的更改并推送到我的fork,以便我可以创建对主repo的pull请求。

I know it is posible to add both remotes using git remote add <name> <url> but I don't want to explicitly do git pull <main_repo> and git push <fork_repo> 我知道使用git remote add <name> <url>添加两个遥控器是可行的,但我不想明确地执行git pull <main_repo>git push <fork_repo>

git remote set-url --push origin <fork_repo>

After doing that you can check urls with this command 执行此操作后,您可以使用此命令检查URL

git remote -v

and you'll see something like 你会看到类似的东西

origin <main_repo> (fetch)
origin <fork_repo> (push) 

The solution for this is to add the main repository as the "origin" remote: 解决方案是将主存储库添加为“origin”远程:

git remote add origin <main_repository_url>

Or if already exists: 或者如果已经存在:

git remote set-url origin <main_repository_url>

Then set the fork as the push url of the remote: 然后将fork设置为远程的push url:

git remote set-url --push origin <fork_repository_url>

Just to check the results: 只是为了检查结果:

$ git remote -v
origin  <main_repository_url> (fetch)
origin  <fork_repository_url> (push)

And now it is possible to pull/checkout branches from the main repository and push changes to the fork. 现在可以从主存储库中提取/签出分支并将更改推送到分支。

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

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