如何将GIT存储库从一台服务器迁移到一台新服务器

时间:2009-09-27 22:26:26

标签: git

我有一台服务器,我正在取消。我唯一要迁移的是我的存储库。此服务器被列为我的一个项目的源(主)。移动存储库以保留历史记录的正确方法是什么。

16 个答案:

答案 0 :(得分:241)

添加新的仓库位置

git remote add new_repo_name new_repo_url

然后将内容推送到新位置

git push new_repo_name master

最后删除旧的

git remote rm origin

之后,您可以执行bdonlan所说的内容并编辑.git / config文件以将new_repo_name更改为origin。如果不删除原点(原始远程存储库),只需使用

将更改推送到新的存储库即可
git push new_repo_name master

答案 1 :(得分:187)

如果要迁移所有分支和标记,则应使用以下命令:

git clone --mirror [oldUrl]

用所有分支克隆旧的仓库

cd the_repo
git remote add remoteName newRepoUrl

设置新的远程

git push -f --tags remoteName refs/heads/*:refs/heads/*

在refs / heads下推送所有引用(这可能是你想要的)

答案 2 :(得分:116)

这对我有用 完美无缺

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 -f origin

我必须提一下,虽然这会创建一个当前仓库的镜像,然后将其推送到新位置。 因此,大型回购或慢速连接可能需要一些时间

答案 3 :(得分:75)

复制它。这真的很简单。 :)

在客户端,只需在客户端的本地仓库中编辑.git / config,根据需要将您的遥控器指向新的URL。

答案 4 :(得分:48)

在某些其他答案中,这部分是完成的。

git clone --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new

答案 5 :(得分:34)

我只是按照一个简单的说明列表重新发布其他人所说的内容。

  1. 移动存储库:只需登录到新服务器cd即可到达您希望存放存储库的父目录,并使用rsync从旧服务器复制:

    new.server> rsync -a -v -e ssh user@old.server.com:path/to/repository.git .
    
  2. 让客户端指向新的存储库:现在,在使用存储库的每个客户端上,只需删除指向旧源的指针,然后将其添加到新存储库中。

    client> git remote rm origin
    client> git remote add origin user@new.server.com:path/to/repository.git
    

答案 6 :(得分:15)

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

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

像魅力一样工作!

答案 7 :(得分:10)

我按照BitBucket上的说明移动了一个包含所有分支的回购。以下是#字符后面的解释步骤:

cd path/to/local/repo
git remote remove origin # to get rid of the old setting, this was not in the BitBucket instructions
git remote add origin ssh://git@bitbucket.org/<username>/<newrepo> # modify URL as needed
git push -u origin --all # pushes _ALL_ branches in one go
git push -u origin --tags # pushes _ALL_ tags in one go

很适合我。

答案 8 :(得分:6)

请按照以下步骤操作:

  • git remote add new-origin
  • git push --all new-origin
  • git push --tags new-origin
  • git remote rm origin
  • git remote rename new-origin origin

答案 9 :(得分:4)

您可以使用以下命令:

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)

答案 10 :(得分:4)

应该如此简单:

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

通过这种方式,您可以为新的仓库保留名称origin - 然后按照其他答案中的详细说明将新旧仓库推送到新仓库。假设你单独工作并且你有一个当地的回购,你想要镜像它的所有内容,你也可以(从你当地的回购中)

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

但请参阅Is "git push --mirror" sufficient for backing up my repository?(总共不要使用--mirror但是一次)。

答案 11 :(得分:2)

按照这些说明进行操作如果要保留所有提交和分支,从旧到新的回购

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

答案 12 :(得分:1)

您可以使用git-copy复制包含所有历史记录的回购。

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

答案 13 :(得分:1)

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

  1. 让我们假设您的旧项目名为existing_repo,存储在existing repo文件夹中。

  2. 在新服务器上创建存储库。我们将假定该新项目的URL为git@newserver:newproject.git

  3. 打开命令行界面,然后输入以下内容:

    cd existing_repo
    git remote rename origin old-origin
    git remote add origin git@newserver:newproject.git
    git push -u origin --all
    git push -u origin --tags
    

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

答案 14 :(得分:0)

如果要将#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

只会将主分支(仅跟踪分支)复制到新存储库。

答案 15 :(得分:0)

如果您想从一个原点移动到另一个原点并在本地计算机上保留当前原点的备份,您可以使用以下步骤:

  1. 首先在本地转到要移动的(git)文件夹
  2. 在线创建新存储库 此步骤创建了一个存储库,我们可以将代码推送到
  3. 现在在文件夹中执行

    git remote get-url origin
    

    上面的命令给出了当前的远程原始URL,用于将原点设置回到最后一步

    git remote set-url origin git@github.newlocation:folder/newrepo.git
    

    以上命令将远程原点设置为新位置

    git push --set-upstream origin develop
    

    上面的命令通过branchname develop将当前活动的本地分支推送到远程。当然,它保留所有历史,就像git所有历史也被推动。

    git remote set-url origin <original old origin>
    

    上面的命令将远程原点设置回当前原点:您想要这样,因为您在现有文件夹中,并且您可能不希望将当前本地文件夹名称与要为其创建的新文件夹混淆克隆刚推到的仓库。

    希望这有帮助,

相关问题