简体   繁体   English

推送到不同的 git 回购

[英]Pushing to a different git repo

I have a repo called react .我有一个名为react的回购协议。 I cloned it into a different repo locally called different-repo .我将它克隆到本地称为different-repo的不同 repo 中。

How can I then get different-repo to push remotely to different-repo because currently it is pushing to react .我怎样才能让different-repo远程推送到 different-repo 因为目前它正在推动react

Effectively I want to clone many times from react into different named repos but then when i push from those repos they push to their own repo.实际上,我想多次从react克隆到不同的命名 repos,但是当我从这些 repos 推送时,它们会推送到他们自己的 repo。

You have to add an other remote .您必须添加另一个remote Usually, you have an origin remotes, which points to the github (maybe bitbucket) repository you cloned it from.通常,您有一个origin remotes,它指向您从中克隆它的 github(可能是 bitbucket)存储库。 Here's a few example of what it is:以下是它的一些示例:

  • https://github.com/some-user/some-repo (the .git is optional) https://github.com/some-user/some-repo (该.git是可选的)
  • git@github.com:some-user/some-repo (this is ssh, it allows you to push/pull without having to type your ids every single time) git@github.com:some-user/some-repo (这是 ssh,它允许您推/拉而无需每次输入您的 id)
  • C:/some/folder/on/your/computer Yes! C:/some/folder/on/your/computer是的! You can push to an other directory on your own computer.您可以推送到您自己计算机上的其他目录。

So, when you所以,当你

$ git push origin master

origin is replaced with it's value: the url origin被替换为它的值:url

So, it's basically just a shortcut .所以,它基本上只是一个捷径 You could type the url yourself each time, it'd do the same!你可以每次都自己输入网址,它会做同样的事情!

Note : you can list all your remote s by doing git remote -v .注意:您可以列出你所有的remote通过执行s git remote -v

For your problem对于您的问题

How can I then get different-repo to push remotely to different-repo because currently it is pushing to react.我怎样才能让不同的回购远程推送到不同的回购,因为目前它正在推动做出反应。

I'm guessing you want to create a second repository, right?我猜您想创建第二个存储库,对吗? Well, you can create an other remote (or replace the current origin ) with the url to this repo!好吧,您可以使用此 repo 的 url 创建另一个remote (或替换当前的origin )!

Add an other remote — recommended添加另一个remote - 推荐

git remote add <remote-name> <url>

So, for example:因此,例如:

$ git remote add different-repo https://github.com/your-username/your-repo

And then, just然后,只要

$ git push different-repo master

Change the origin remote更改origin remote

git remote set-url <remote-name> <url>

So所以

git remote set-url origin https://github.com/your-username/your-repo

Here different-repo is the first repo from which you created/cloned the child repo react这里不同的回购是您创建/克隆子回购反应的第一个回购

So by default child repo react will have its default remote as different-repo where you can push/pull changes.因此,默认情况下,子 repo react 将其默认远程作为不同的 repo,您​​可以在其中推送/拉取更改。

Here child repo will maintain all the commit history of parent repo within its .git folder这里子仓库将在其 .git 文件夹中维护父仓库的所有提交历史

If you want to push the changes to different repo from this react repo then add another remote(you can add as many as remotes here and also can delete the old remotes)如果您想将更改从这个 react repo 推送到不同的 repo,然后添加另一个遥控器(您可以在此处添加尽可能多的遥控器,也可以删除旧遥控器)

Add new Remote to react添加新的遥控器以做出反应

git remote add <remote-name> <url>

If you want to remove the old remote如果你想删除旧的遥控器

git remote remove <remote_name>

Git push to new repo from existing repo's branch Git 从现有仓库的分支推送到新仓库

This blog is to the point and explains it very well.这个博客很中肯,并且解释得很好。 Here is the snippet from the blog.这是博客的片段。

Go to current project: 
$ cd my-project

Add new origin (origin2): git remote add origin2 <git_url>
$ git remote add origin2 https://github.com/my-org/new-project

The following command pushes master branch of current repo to master branch of new repo with remote configured as origin2.
$ git push <remote_name> <remote_repo_branch>
$ git push origin2 master

The following command pushes specific branch (say dev) of current repo to master branch of new repo with remote configured as origin2.
$ git push origin2 <source_branch>:<destination_branch>
$ git push origin2 dev:master
Use --force (to forcefully push into that new branch if required)
$ git push origin2 <source_branch>:<destination_branch> --force

Note that if your current repo's branch that you want to push is called master and the remote repo branch where you push is called main , you should do git push different-repo master:main请注意,如果您当前要推送的回购分支称为master并且您推送的远程回购分支称为main ,您应该执行git push different-repo master:main

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

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