我正在尝试使用以下命令将一个存储库(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
但它没有起作用。我查阅了一个类似的帖子,但我只找到了一个移动文件夹而不是其内容的方法。
我认为你正在寻找的命令是:
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
的所有内容,并且还将拥有它们两者的历史记录。
repo1
的代码提取/合并到repo2
的特定目录中?目前,它正在提取repo2
根目录中的所有内容。 - abhijithda--allow-unrelated-histories
是在 git-2.9.0
中引入的,旧版本的 git
不需要该选项(例如 centos 7 :D)。 - Samveen这里完美地描述了: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,以便它成为默认远程版本库:
将new-origin重命名为origin,以便它成为默认远程版本库:
git remote rename new-origin origin
git checkout BranchName
命令切换分支,然后再用 git push --all new-origin
命令将分支推送到远程仓库。非常感谢。 - Raad Altaie如果您想保留现有的分支和提交历史记录,这是我成功实践过的一种方法。
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
注意:我还没有使用子模块,所以如果您使用了它们,可能需要额外的步骤。
这个方法成功地将我的本地仓库(包括历史记录)移动到我的远程 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
如果代码已被Git跟踪,则将新存储库设置为要推送到的“origin”即可。
cd existing-project
git remote set-url origin https://clone-url.git
git push -u origin --all
git push origin --tags
根据@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
git clone --bare <URL>
而不是git clone --mirror
。因为如果您镜像了一个github存储库,它将不仅克隆与源代码相关的内容,还会克隆剩余的拉取请求和github引用,导致推送时出现![remote rejected]
错误。
我在谈论这个问题
我推荐的步骤如下:git clone --bare <旧仓库URL>
2. cd <克隆仓库的路径>
3. git push --mirror <新仓库URL>
成功将所有分支、历史记录和源代码迁移到github新存储库,没有任何错误!我做的事情:
cd existing-project
git remote set-url origin <NEW_GIT_REPO>
git push -u origin --all
git push origin --tags
git clone --bare <STASH-URL>
git push --mirror <GitLab-URL>