207

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

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

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

4

14 回答 14

348

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

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/master和的所有内容repo1/master,并且还将包含它们两者的历史。

于 2013-06-28T20:25:22.093 回答
97

在这里完美描述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
于 2016-07-11T09:43:54.747 回答
39

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

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

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

于 2017-03-28T18:50:53.007 回答
18

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

cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags
于 2019-04-24T22:27:07.093 回答
16

这有助于将我的本地存储库(包括历史记录)移动到我的远程 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

于 2017-08-25T21:04:37.193 回答
8

镜像存储库

根据@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

于 2019-04-29T16:14:46.393 回答
2

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

将旧存储库克隆到本地。

git clone --bare <STASH-URL>

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

git push --mirror <GitLab-URL>
于 2019-04-08T01:36:22.737 回答
1

看起来你很近。假设这不仅仅是您提交的拼写错误,那么第 3 步应该cd repo2代替 repo1。并且第 6 步不应该被git pull推动。重做清单:

  1. git clone repo1
  2. git clone repo2
  3. cd repo2
  4. git remote rm origin
  5. git remote add repo1
  6. git pull
  7. git remote rm repo1
  8. git remote add newremote
于 2013-06-28T18:24:26.793 回答
1

我做了什么:

  1. 将存储库克隆到文件夹
  2. cd existing-project
  3. 在这里打开一个 git 终端
  4. git remote set-url origin <NEW_GIT_REPO>
  5. git push -u origin --all
  6. git push origin --tags
于 2021-04-28T07:53:27.293 回答
1

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

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

我说的是这个问题

我推荐的程序:

  1. git clone --bare <Old Repo Url>
  2. cd <path to cloned repo>
  3. git push --mirror <New Repo Url>

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

于 2021-06-14T13:25:54.720 回答
0

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

https://github.com/new/import

在此处输入图像描述

于 2021-10-27T09:41:23.947 回答
0

我在这里看到了很多解决方案。过去对我有用的一种快速单行器是使用 --mirror 选项。首先进入您要复制的存储库。然后假设你已经设置了一个新的仓库,使用克隆链接就可以了。它会将所有历史记录复制到您的新仓库中。

cd repoOne

git --mirror linktonewrepo.git

于 2022-02-23T21:41:26.947 回答
-1

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

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

cd <location of local repo.>
git remote set-url origin <url>
git push -u origin master
于 2016-07-22T19:35:17.193 回答
-1

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

于 2021-01-28T14:31:43.023 回答