当前位置: 代码迷 >> 综合 >> How to migrate a git repository from one server to another
  详细解决方案

How to migrate a git repository from one server to another

热度:67   发布时间:2023-12-15 00:42:26.0

??软件开发过程中,我们经常会遇到需要对git仓库迁移的情况,有没有什么好的方式完成这项工作呢,其实是有的,git仓库的迁移工作主要分为以下两种场景:

  • 整体迁移

??整体迁移就是将git服务器上的所有仓库整体迁移到另一个服务器中。

??对于这种场景,我们只需要在git服务器上找到存储仓库的数据目录,将其打包传输到目标服务器上就可以了。其局限性在于只有git服务器管理员才可以有权限操作。

  • 部分迁移

??部分迁移就是将git服务器上的部分仓库迁移到另一个服务器中去。

??对于这种场景,我们需要借助git工具的命令git clone --bare urlgit push --mirror url来完成。不足之处,需要一个仓库一个仓库单独进行迁移。好处在于不需要git服务器管理员的直接参与。

??接下来,我们着重介绍一下部分迁移的工作流程。

在这里插入图片描述
??这幅图展示了,git操作员将git仓库从git服务器1迁移到git服务器2的业务场景。下面详细说明工作流程。

??1、git仓库操作员需要向git服务器管理员申请一个git仓库,git服务器管理员创建好一个空的git仓库并将仓库地址url2给到git仓库操作员。

??2、git仓库操作员将服务器1中的git仓库检出(clone)到本地,借助如下命令:

git clone --bare url1

??3、git仓库操作员将本地git仓库推送到服务器2上,借助如下命令:

git push --mirror url2

??接下来,我们跑一个真实案例给大家,这样大家就明白了。

第一步,从192.168.100.146:29418/hello-world.git检出代码,具体如下:

lwk@qwfys ~/Public/project/qwfys $ git clone --bare ssh://git@192.168.100.146:29418/hello-world.git
Cloning into bare repository 'hello-world.git'...
remote: Counting objects: 18537, done
remote: Finding sources: 100% (18537/18537)
remote: Getting sizes: 100% (12864/12864)
remote: Total 18537 (delta 5320), reused 18537 (delta 5320)
Receiving objects: 100% (18537/18537), 174.13 MiB | 6.28 MiB/s, done.
Resolving deltas: 100% (5320/5320), done.
Checking connectivity... done.
lwk@qwfys ~/Public/project/qwfys $

第二步,进入到目录hello-world.git中,推送仓库到新的目标仓库,具体如下:

lwk@qwfys ~/Public/project/qwfys $ cd hello-world.git/
lwk@qwfys ~/Public/project/qwfys/hello-world.git $ ll
total 44
drwxrwxr-x 7 lwk lwk 4096 Mar 16 10:20 ./
drwxrwxr-x 3 lwk lwk 4096 Mar 16 10:20 ../
drwxrwxr-x 2 lwk lwk 4096 Mar 16 10:20 branches/
-rw-rw-r-- 1 lwk lwk  145 Mar 16 10:20 config
-rw-rw-r-- 1 lwk lwk   73 Mar 16 10:20 description
-rw-rw-r-- 1 lwk lwk   23 Mar 16 10:20 HEAD
drwxrwxr-x 2 lwk lwk 4096 Mar 16 10:20 hooks/
drwxrwxr-x 2 lwk lwk 4096 Mar 16 10:20 info/
drwxrwxr-x 4 lwk lwk 4096 Mar 16 10:20 objects/
-rw-rw-r-- 1 lwk lwk  643 Mar 16 10:20 packed-refs
drwxrwxr-x 4 lwk lwk 4096 Mar 16 10:20 refs/
lwk@qwfys ~/Public/project/qwfys/hello-world.git $ git push --mirror git@192.168.100.42:qwfys/hello-world.git
Counting objects: 18537, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (10327/10327), done.
Writing objects: 100% (18537/18537), 174.13 MiB | 7.92 MiB/s, done.
Total 18537 (delta 5320), reused 18537 (delta 5320)
remote: Resolving deltas: 100% (5320/5320), done.
remote: 
remote: To create a merge request for 20171130_wz_productService, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=20171130_wz_productService
remote: 
remote: To create a merge request for dev-zyj, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=dev-zyj
remote: 
remote: To create a merge request for develop, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote: 
remote: To create a merge request for fy_flowperformance_20180106, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=fy_flowperformance_20180106
remote: 
remote: To create a merge request for release, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=release
remote: 
remote: To create a merge request for shq_20171220, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=shq_20171220
remote: 
remote: To create a merge request for spring5_dev_fy_20171102, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=spring5_dev_fy_20171102
remote: 
remote: To create a merge request for zyj-1201-dev, visit:
remote:   http://192.168.100.42/qwfys/hello-world/merge_requests/new?merge_request%5Bsource_branch%5D=zyj-1201-dev
remote: 
To git@192.168.100.42:qwfys/hello-world.git* [new branch]      20171130_wz_productService -> 20171130_wz_productService* [new branch]      dev-zyj -> dev-zyj* [new branch]      develop -> develop* [new branch]      fy_flowperformance_20180106 -> fy_flowperformance_20180106* [new branch]      master -> master* [new branch]      release -> release* [new branch]      shq_20171220 -> shq_20171220* [new branch]      spring5_dev_fy_20171102 -> spring5_dev_fy_20171102* [new branch]      zyj-1201-dev -> zyj-1201-dev
lwk@qwfys ~/Public/project/qwfys $

??这样就完成了一个git仓库的迁移操作,我们看到,这种操作连同仓库所有分支一起迁移了过去。

  相关解决方案