代码之家  ›  专栏  ›  技术社区  ›  cjibo

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

git
  •  236
  • cjibo  · 技术社区  · 16 年前

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

    16 回复  |  直到 7 年前
        1
  •  235
  •   Koby    16 年前

    要添加新的回购位置,

    git remote add new_repo_name new_repo_url
    

    然后将内容推到新位置

    git push new_repo_name master
    

    最后去掉旧的

    git remote rm origin
    

    之后,您可以按照bdonlan所说的操作,编辑.git/config文件,将新的_repo_名称更改为origin。如果不删除源站(原始远程存储库),只需使用

    Git推送新回购名称主机
    
        2
  •  174
  •   jzwiener    12 年前

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

    git clone --mirror [oldUrl]
    

    用所有分支克隆旧的repo

    cd the_repo
    git remote add remoteName newRepoUrl
    

    设置新的遥控器

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

    将所有引用推到引用/头下(这可能是您想要的)

        3
  •  109
  •   Roberto    11 年前

    这对我有用 完美无瑕 .

    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
    

    不过,我必须提到,这将为您当前的回购创建一面镜子,然后将其推到新的位置。 因此,大型回购或慢速连接可能需要一些时间 .

        4
  •  76
  •   bdonlan    16 年前

    抄一遍。这真的很简单。:)

    在客户端,只需在客户端的本地repo中编辑.git/config,就可以根据需要将远程设备指向新的URL。

        5
  •  44
  •   Ian Walters    10 年前

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

    git clone --mirror git@oldserver:oldproject.git
    cd oldproject.git
    git remote add new git@newserver:newproject.git
    git push --mirror new
    
        6
  •  33
  •   Juan A. Navarro    13 年前

    我只是把别人说的话放在一个简单易懂的指示列表中。

    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
      
        7
  •  15
  •   John Smith    12 年前

    看看Github上的这个配方: https://help.github.com/articles/importing-an-external-git-repository

    我尝试了很多方法才发现 git push --mirror .

    工作得很有魅力!

        8
  •  10
  •   András Aszódi    12 年前

    我按照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
    

    为我工作得很好。

        9
  •  6
  •   Boush    12 年前

    请遵循以下步骤:

    • git远程添加新来源
    • git push——所有新来源
    • git push—标记新原点
    • Git远程RM源站
    • Git远程重命名新原点
        10
  •  4
  •   mrroboaat    13 年前

    可以使用以下命令:

    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)
    
        11
  •  4
  •   Community Mohan Dere    9 年前

    应该简单到:

    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 但一次。

        12
  •  2
  •   Quanlong    11 年前

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

    git copy http://a.com/old.git http://a.com/new.git
    
        13
  •  2
  •   Extreme    9 年前

    如果您希望保留从旧回购到新回购的所有承诺和分支,请遵循这些说明。

    git clone --bare <old-repo-url>
    cd <old-repo-directory>
    git push --mirror <new-repo-url>
    
        14
  •  1
  •   Clément    7 年前

    这是对 this answer ,当前建议者 gitlab 将Git存储库从一台服务器“迁移”到另一台服务器。

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

    2. 在新服务器上创建repo。我们假设这个新项目的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
      

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

        15
  •  0
  •   maestr0    12 年前

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

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

        16
  •  0
  •   Hace    8 年前

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

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

    现在在文件夹中做

    git remote get-url origin
    

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

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

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

    git push --set-upstream origin develop
    

    上面的命令将当前活动的本地分支推送到远程,并开发branchname。当然,它保留了所有的历史,就像Git一样,所有的历史都被推送了。

    git remote set-url origin <original old origin>
    

    上面的命令将远程原点设置回当前原点:您希望这样做是因为您位于现有文件夹中,并且可能不希望将当前本地文件夹名与要创建的新文件夹混淆,以便克隆刚刚推到的repo。

    希望这有帮助,