我正在尝试使用以下命令将一个存储库(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
但这并不奏效。我看过一篇类似的文章,但我只发现一个人移动了文件夹,而不是内容。
当前回答
完美描述在这里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为just origin,使其成为默认远程:
git remote rename new-origin origin
其他回答
如果您希望保留现有的分支并提交历史,这里有一种对我有用的方法。
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
注:我还没有使用子模块,所以我不知道如果你有他们可能需要额外的步骤。
我做了什么:
克隆存储库到一个文件夹 cd现有项目 这里打开一个git终端 git remote set-url origin <NEW_GIT_REPO> Git push -u origin——all Git push origin—tags
完美描述在这里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为just origin,使其成为默认远程:
git remote rename new-origin origin
对于那些使用GitHub的人,你可以通过web UI导入另一个存储库(包括历史):
https://github.com/new/import
我不太确定我所做的是否正确,但无论如何它都起作用了。我重命名了repo1中的repo2文件夹,并提交了更改。在我的情况下,这只是一个回购,我想合并,所以这种方法是有效的。后来,进行了一些路径更改。