我正在尝试使用以下命令将一个存储库(repo1)的内容移动到另一个现有的存储库(repo2):
git clone repo1
git clone repo2
cd repo1
git remote rm origin
git remote add repo1
git push
但这并不奏效。我看过一篇类似的文章,但我只发现一个人移动了文件夹,而不是内容。
我正在尝试使用以下命令将一个存储库(repo1)的内容移动到另一个现有的存储库(repo2):
git clone repo1
git clone repo2
cd repo1
git remote rm origin
git remote add repo1
git push
但这并不奏效。我看过一篇类似的文章,但我只发现一个人移动了文件夹,而不是内容。
当前回答
镜像存储库
根据@Dan-Cohn的回答,镜像推送是你的朋友。这是我的迁移回购:
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
其他回答
最简单的方法是,如果代码已经被Git跟踪,那么将新的存储库设置为要推送的“源”。
cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags
看起来你很接近了。假设不仅仅是提交一个错字,应该cd repo2代替repo1步骤3。第六步应该是拉而不是推。修改了列表:
Git克隆repo1 Git克隆repo2 cd repo2 Git远程rm来源 Git远程添加repo1 git拉 Git远程rm repo1 Git远程添加newremote
如果您希望保留现有的分支并提交历史,这里有一种对我有用的方法。
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
注:我还没有使用子模块,所以我不知道如果你有他们可能需要额外的步骤。
镜像存储库
根据@Dan-Cohn的回答,镜像推送是你的朋友。这是我的迁移回购:
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
我认为您正在寻找的命令是:
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的所有内容,并且还将包含它们的历史。