我有个服务器要关闭。我剩下要迁移的唯一东西是我的存储库。这个服务器被列为我的一个项目的原始(主)服务器。移动存储库以保存历史的正确方法是什么?


当前回答

如果你想保持所有的提交和分支从旧到新的回购

git clone --bare <old-repo-url>
cd <old-repo-directory>
git push --mirror <new-repo-url>

其他回答

更新为使用git push——mirror origin而不是评论中建议的git push -f origin。


这个方法对我来说完美无缺。

git clone --mirror <URL to my OLD repo location>
cd <New directory where your OLD repo was cloned>
git remote set-url origin <URL to my NEW repo location>
git push --mirror origin

我不得不提的是,这将创建当前回购的镜像,然后将其推到新位置。因此,对于大型回购或慢速连接,这可能需要一些时间。

如果你想保持所有的提交和分支从旧到新的回购

git clone --bare <old-repo-url>
cd <old-repo-directory>
git push --mirror <new-repo-url>

可以使用如下命令:

git remote set-url --push origin new_repo_url

例子来自http://gitref.org/remotes/

$ git remote -v
github  git@github.com:schacon/hw.git (fetch)
github  git@github.com:schacon/hw.git (push)
origin  git://github.com/github/git-reference.git (fetch)
origin  git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github  git@github.com:schacon/hw.git (fetch)
github  git@github.com:schacon/hw.git (push)
origin  git://github.com/github/git-reference.git (fetch)
origin  git://github.com/pjhyett/hw.git (push)

备注:

git copy http://a.com/old.git http://a.com/new.git

仅适用于例如,一个github到github拷贝,即保留在相同的git系统。当从例如github复制到gerrit时,这是不工作的。

除此之外,它还很好用,因为它自动复制分支、标记和子模块。

如果你想把一个#git仓库从一个服务器迁移到一个新的服务器,你可以这样做:

git clone OLD_REPOSITORY_PATH
cd OLD_REPOSITORY_DIR
git remote add NEW_REPOSITORY_ALIAS  NEW_REPOSITORY_PATH
#check out all remote branches 
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git push --mirror NEW_REPOSITORY_PATH
git push NEW_REPOSITORY_ALIAS --tags

旧存储库中的所有远程分支和标记将被复制到新的存储库中。

单独运行该命令:

git push NEW_REPOSITORY_ALIAS

只复制一个主分支(只跟踪分支)到新的存储库。