繁体   English   中英

如何在 git 中将一个仓库转移到另一个仓库

[英]How to transfer one repo to another repo in git

我想将我所有的回购 A 复制到回购 B 以及所有历史详细信息。 我正在遵循以下命令

git clone --mirror old-repo-url new-repo
cd new-repo
git remote remove origin
git remote add origin new-repo-url
git push --all

当我尝试推送 git push --all 命令时出现以下错误

! [rejected]          main -> main (fetch first)
error: failed to push some refs to 'https://git.repoB'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

当我尝试运行 git pull 命令时,系统提示“致命:此操作必须在工作树中运行”。 谁能告诉如何解决这个问题?

参考 - https://itnext.io/git-repository-transfer-keeping-all-history-670fe04cd5e4

您尝试git push的存储库(您的“repo B”)已经有一些提交 您的git push清除 repo B 中的历史记录 因此,它对您的请求说“不”。

请记住,在 Git 中,历史记录您可以在存储库中找到的一组提交。 就是这样! 分支和标签名称只是查找特定提交的方法。 每个提交都有一些元数据,并且在任何一个给定提交的元数据中,Git 都存储了原始哈希 ID——实际上是“真实名称”——以前提交的列表。 分支名称包含最新提交的哈希 ID(根据定义:无论分支名称中的哈希 ID 是什么,该提交都是最新提交)。 因此,历史记录是通过从各种名称开始并向后工作而找到的一组提交。

在您的情况下,您从 repo A 复制了所有名称和提交,以便您的新克隆具有以相同名称找到的相同提交集:即相同的历史记录。 但是然后你告诉你的 Git 软件git push --all到 repo B,它将提交发送repo B,然后要求 repo B设置它的所有名称 如果他们已经有自己的提交——而且他们确实有——那么对于他们来说,设置他们的名字main ,在这种情况下,将失去他们的提交 换句话说,它失去了他们的历史。

要以这种方式复制存储库,我们通常希望将git push到一个完全空的存储库 这意味着在我们git push到 repo B 之前,我们必须首先将 repo B创建为一个空的 repository 那是一个没有分支也没有提交的存储库,因此请确保如果您正在使用例如 GitHub 的“创建新存储库”Web 界面,请选择“创建完全空的存储库”选项。

暂无
暂无

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

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