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


当前回答

应该像这样简单:

git remote set-url origin git://new.url.here

通过这种方式,您可以保留新回购的名称来源-然后将旧的推到新回购,如其他答案中详细说明的那样。假设你独自工作,你有一个本地回购,你想镜像所有的cruft,你也可以(从你的本地回购内部)

git push origin --mirror # origin points to your new repo

但请参阅“git push——mirror”是否足以备份我的存储库?(在所有不使用——镜子但一次)。

其他回答

可以使用如下命令:

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 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 --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new

看看GitHub上的这个食谱: https://help.github.com/articles/importing-an-external-git-repository

在发现git push -mirror之前,我尝试了许多方法。

效果好极了!

这是这个答案的一个变体,目前gitlab建议将一个git存储库从一个服务器“迁移”到另一个服务器。

让我们假设您的旧项目名为existing_repo,存储在existing_repo文件夹中。 在新服务器上创建一个回购。我们假设这个新项目的url是git@newserver:newproject.git 打开命令行界面,输入如下信息: cd existing_repo Git远程重命名原点old-origin Git远程添加源git@newserver:newproject.git Git push -u origin——all Git push -u origin——tags

这种方法的好处是不需要删除与旧服务器相对应的分支。