Git将现有的repo推送到一个新的不同的远程repo服务器?

问题描述 投票:401回答:9

假设我在git.fedorahosted.org上有一个存储库,我想将它克隆到我在github的帐户中,除了fedorahosted上更“官方”的回购之外,还有我自己的游乐场。最初复制它的步骤是什么?在github中有一个很好的“fork”按钮,但由于显而易见的原因,我无法使用它。

我如何跟踪fedorahosted repo中的变化到github one?

git github
9个回答
647
投票
  1. 在github上创建一个新的repo。
  2. 将repora从fedorahosted克隆到您的本地计算机。
  3. git remote rename origin upstream
  4. git remote add origin URL_TO_GITHUB_REPO
  5. git push origin master

现在你可以像任何其他github repo一样使用它。要从上游提取补丁,只需运行git pull upstream master && git push origin master


89
投票

这个问题有一个删除的答案,它有一个有用的链接:https://help.github.com/articles/duplicating-a-repository

要点是

0. create the new empty repository (say, on github)
1. make a bare clone of the repository in some temporary location
2. change to the temporary location
3. perform a mirror-push to the new repository
4. change to another location and delete the temporary location

OP的例子:

在您的本地计算机上

$ cd $HOME
$ git clone --bare https://git.fedorahosted.org/the/path/to/my_repo.git
$ cd my_repo.git
$ git push --mirror https://github.com/my_username/my_repo.git
$ cd ..
$ rm -rf my_repo.git

37
投票

要将现有的仓库推送到不同的仓库,您需要:

  1. 首先克隆原始回购。 git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
  2. 将克隆的源推送到新的存储库: cd rhq git push https://github.com/user/example master:master

你可以将master:master改成source:destination分支。


如果要推送特定的提交(分支),那么执行:

  1. 在原始仓库中,创建并签出新分支: git checkout -b new_branch
  2. 选择并重置为您要开始的点: git log # Find the interesting hash git reset 4b62bdc9087bf33cc01d0462bf16bbf396369c81 --hard 或者,选择git cherry-pick提交以附加到现有HEAD中。
  3. 然后推送到您的新回购: git push https://github.com/user/example new_branch:master 如果您正在变基础,请使用-f进行强制推动(不推荐)。运行git reflog以查看更改历史记录。

12
投票

你真的想简单地将你的本地存储库(带有本地分支机构等)推送到新的遥控器,还是你真的想要在新的遥控器上镜像旧遥控器(及其所有分支,标签等)?如果后者在这里是关于How to properly mirror a git repository的一个很棒的博客。

我强烈建议您阅读博客中的一些非常重要的细节,但简短的版本是这样的:

在新目录中运行以下命令:

git clone --mirror [email protected]/upstream-repository.git
cd upstream-repository.git
git push --mirror [email protected]/new-location.git

8
投票

试试这个How to move a full Git repository

  1. 使用以下命令在temp-dir目录中创建本地存储库: git clone temp-dir
  2. 进入temp-dir目录。
  3. 要查看ORI中不同分支的列表,请执行以下操作: git branch -a
  4. 使用以下命令检出要从ORI复制到NEW的所有分支: git checkout branch-name
  5. 现在使用以下方法从ORI获取所有标记: git fetch --tags
  6. 在执行下一步之前,请确保使用以下命令检查本地标记和分支: git tag git branch -a
  7. 现在使用以下命令清除指向ORI存储库的链接: git remote rm origin
  8. 现在使用以下命令将本地存储库链接到新创建的新存储库: git remote add origin <url to NEW repo>
  9. 现在使用以下命令推送所有分支和标记: git push origin --all git push --tags
  10. 您现在拥有ORI回购的完整副本。

7
投票

我找到了一个使用set-url的解决方案,它简洁易懂:

  1. 在Github创建一个新的回购
  2. cd进入本地计算机上的现有存储库(如果尚未克隆它,请先执行此操作)
  3. git remote set-url origin https://github.com/user/example.git
  4. git push -u origin master

3
投票

如果您有Existing Git存储库:

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/newproject
git push -u origin --all
git push -u origin --tags

2
投票

我曾经也有过一样的问题。

在我的情况下,因为我在本地机器中有原始存储库,所以我在新文件夹中制作了一个没有任何隐藏文件(.git,.gitignore)的副本。

最后,我将.gitignore文件添加到新创建的文件夹中。

然后我从本地路径创建并添加了新的存储库(在我的例子中使用GitHub Desktop)。


0
投票

只需使用以下命令更改GIT repo URL即可指向新的repo:

git remote set-url origin [new repo URL]

示例:git remote set-url origin [email protected]:Batman/batmanRepoName.git

现在,推动和拉动与新的REPO相关联。

然后像这样正常推送:

git push -u origin master
© www.soinside.com 2019 - 2024. All rights reserved.