将 Git 存储库内容移动到另一个存储库,保留历史记录

Posted

技术标签:

【中文标题】将 Git 存储库内容移动到另一个存储库,保留历史记录【英文标题】:Moving Git repository content to another repository preserving history 【发布时间】:2013-06-26 14:34:33 【问题描述】:

我正在尝试使用以下命令仅将一个存储库 (repo1) 的内容移动到另一个现有存储库 (repo2):

git clone repo1
git clone repo2
cd repo1
git remote rm origin
git remote add repo1
git push

但它不起作用。我查看了一篇类似的帖子,但我只发现一个移动文件夹,而不是内容。

【问题讨论】:

你想让 repo1 的内容作为 repo2 上的一个分支,还是作为 master 的一部分,这样两个 repos 的文件夹共存于你的工作目录中? 我想把它作为master的一部分移动。 这个教程很完美! smashingmagazine.com/2014/05/19/… How to move files from one git repo to another (not a clone), preserving history的可能重复 对于GitHub用户,您可以通过github.com/new/import导入另一个repo 【参考方案1】:

我认为您正在寻找的命令是:

cd repo2
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote

之后repo2/master 将包含来自repo2/masterrepo1/master 的所有内容,并且还将包含两者的历史记录。

【讨论】:

感谢 Chronial,我只是想确保 repo1 也有一个分支,如果我想将该分支也移动到 repo2 怎么办? 慢性,以上命令无效!它没有复制历史。 好的,现在可以了。我使用了以下命令;cd repo2,> git remote rm origin > git remote add origin url-of-repo1,> git fetch r1remote > git push origin master @Chronial 有没有办法将repo1 的代码提取/合并到repo2 的特定目录中?目前正在提取repo2的根目录下的所有内容? @abhijithda 最简单的解决方案是在合并之前将repo1 中的所有内容移动到子文件夹(repo1 中)。【参考方案2】:

在这里完美描述https://www.smashingmagazine.com/2014/05/moving-git-repository-new-server/

首先,我们必须从现有存储库中获取所有远程分支和标签到我们的本地索引:

git fetch origin

我们可以检查我们需要创建本地副本的任何缺少的分支:

git branch -a

让我们使用新存储库的 SSH 克隆 URL 在现有本地存储库中创建一个新远程:

git remote add new-origin git@github.com:manakor/manascope.git

现在我们准备将所有本地分支和标签推送到名为 new-origin 的新远程:

git push --all new-origin 
git push --tags new-origin

让我们将 new-origin 设为默认遥控器:

git remote rm origin

将 new-origin 重命名为 origin,使其成为默认远程:

git remote rename new-origin origin

【讨论】:

可以确认这复制了整个历史和标签。很不错。谢谢。 如果 new-origin 没有任何提交,它运行良好。但是,如果在新源中有任何提交,这里提到的方式不会像预期的那样。 您需要使用git checkout BranchName 切换分支,然后使用git push --all new-origin 再次将分支推送到远程仓库,但非常感谢 获取所有分支到本地然后添加远程:***.com/a/21189710/9247792 如果您已经使用 LFS,这似乎不起作用..【参考方案3】:

如果您希望保留现有的分支和提交历史记录,这是一种对我有用的方法。

git clone --mirror https://github.com/account/repo.git cloned-repo
cd cloned-repo
git push --mirror URL of new (empty) repo

# at this point only remote cloned-repo is correct, local has auto-generated repo structure with folders such as "branches" or "refs"
cd ..
rm -rf cloned-repo
git clone URL of new (empty) repo
# only now will you see the expected user-generated contents in local cloned-repo folder

# note: all non-master branches are avaialable, but git branch will not show them until you git checkout each of them
# to automatically checkout all remote branches use this loop:
for b in `git branch -r | grep -v -- '->'`; do git branch --track $b##origin/ $b; done

现在,假设您想在一段时间内保持源代码库和目标代码库同步。例如,您希望将当前远程仓库中的活动带到新的/替换仓库中。

git clone -o old https://github.com/account/repo.git my-repo
cd my-repo
git remote add new URL of new repo

拉取最新更新(假设您没有本地更改):

git checkout branch(es) of interest
git pull old
git push --all new

注意:我还没有使用子模块,所以我不知道如果你有它们可能需要什么额外的步骤。

【讨论】:

这对我来说很简单。我认为这应该是经过检查的答案。如果你有 60 家分店,这就是你要走的路。 用于提及远程分支和获取它们的命令【参考方案4】:

如果代码已经被 Git 跟踪,那么最简单的方法就是将新的存储库设置为要推送到的“源”。

cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags

【讨论】:

我错误地删除了 original-repo 远程并添加了 new-repo 远程,而没有复制提交历史记录。因此,我对原始存储库所做的所有更改都在新存储库中丢失了。这就像一个魅力,新的回购有我所有的变化。干杯伙伴!【参考方案5】:

这有助于将我的本地存储库(包括历史记录)移动到我的远程 github.com 存储库。在 GitHub.com 创建新的空仓库后,我使用下面第三步中的 URL,效果很好。

git clone --mirror <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git push new-origin --mirror

我在以下位置找到了这个:https://gist.github.com/niksumeiko/8972566

【讨论】:

这是让原始存储库最简单的方法。它还会复制所有分支。 这也会复制所有的标签。 这是从一个 git 深度克隆到另一个 git 的最佳方法【参考方案6】:

镜像仓库

根据@Dan-Cohn 的回答,Mirror-push 是您的朋友。这是我迁移存储库的目标:

1.打开 Git Bash。

2.创建存储库的裸克隆。

$ git clone --bare https://github.com/exampleuser/old-repository.git

3.镜像推送到新仓库。

$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git

4.删除您在步骤 1 中创建的临时本地存储库。

$ cd ..
$ rm -rf old-repository.git

参考和信用:https://help.github.com/en/articles/duplicating-a-repository

【讨论】:

【参考方案7】:

我使用以下方法通过维护所有分支和提交历史来将我的 GIT Stash 迁移到 GitLab。

将旧存储库克隆到本地。

git clone --bare <STASH-URL>

在 GitLab 中创建一个空存储库。

git push --mirror <GitLab-URL>

【讨论】:

【参考方案8】:

看起来你很接近了。假设这不仅仅是您提交的拼写错误,第 3 步应该是 cd repo2 而不是 repo1。第 6 步应该是 git pull 而不是推送。重做列表:

    git clone repo1 git clone repo2 cd repo2 git remote rm origin git remote add repo1 git pull git remote rm repo1 git remote add newremote

【讨论】:

感谢 Eric,但在第 3 步中,我想删除原始存储库的链接以避免进行任何远程更改。因为我想将内容从 repo1 移动到 repo2。 好的。我已经编辑了我的答案以试图反映这一点。只是为了确保,您正在尝试创建一个存储库 repo2,它是 repo1 的副本,但不保留 repo1 或 origin 作为远程? 不,我不想创建一个新的 repo “repo2”。实际上,我有一个现有的 repo2,其中包含其他内容。现在我想将所有 repo1 内容与历史记录一起移动到 repo2 中。我会将 repo1 保留为空,因为它在服务器中。 你考虑过不使用git吗?看来您可能只是复制文件(通过 gui 或 scp 或 ftp)并手动将它们添加到您的 git 存储库中。它可能效率不高,但可能更简单。 我已经尝试过了,但它留下了历史。我也想移动它的所有历史。【参考方案9】:

我做了什么:

    将存储库克隆到文件夹 cd existing-project 在这里打开一个 git 终端 git remote set-url origin &lt;NEW_GIT_REPO&gt; git push -u origin --all git push origin --tags

【讨论】:

【参考方案10】:

如果您要从 github 存储库迁移到另一个 github 存储库

我建议使用:git clone --bare &lt;URL&gt; 而不是:git clone --mirror 迁移,因为如果您镜像 github 存储库,它会不仅克隆源代码相关的东西而且 克隆剩余的拉取请求和 github 引用,导致推送时出现! [remote rejected] 错误。

I am talking about this problem

我推荐的程序:

    git clone --bare &lt;Old Repo Url&gt; cd &lt;path to cloned repo&gt; git push --mirror &lt;New Repo Url&gt;

成功将所有分支、历史和源代码迁移到 github new repo,没有任何错误!

【讨论】:

【参考方案11】:

对于使用 GitHub 的用户,您可以通过 Web UI 导入另一个存储库(包括历史记录):

https://github.com/new/import

【讨论】:

【参考方案12】:

这里有很多复杂的答案;但是,如果您不关心分支保存,您需要做的就是重置远程源,设置上游,然后推送。

这有助于为我保留所有提交历史记录。

cd <location of local repo.>
git remote set-url origin <url>
git push -u origin master

【讨论】:

【参考方案13】:

我不太确定我所做的是否正确,但它仍然有效。我重命名了 repo1 中的 repo2 文件夹,并提交了更改。就我而言,它只是我想合并的一个仓库,所以这种方法有效。后来,做了一些路径更改。

【讨论】:

以上是关于将 Git 存储库内容移动到另一个存储库,保留历史记录的主要内容,如果未能解决你的问题,请参考以下文章

如何将分支内容移动到另一个存储库保留历史记录并避免复制原始存储库的完整历史记录?

如何将一些文件从一个 git repo 移动到另一个(不是克隆),保留历史记录

sh 将子目录从一个git存储库移动到另一个git存储库的子目录,而不会丢失提交历史记录。

sh 将git存储库及其所有分支,标记移动到新的远程存储库,保留提交历史记录

如何将文件从一个文件夹移动到同一git存储库中的另一个文件夹保留历史记录[重复]

将子目录分离(移动)到单独的 Git 存储库中