我们在使用Git对项目进行管理时,有时候会遇到仓库迁移的需求,如从github迁移到自己搭建的私有Gitlab仓库。迁移步骤如下:
举例:
源库地址:git@github.xxx.com/groups:test.com.git
目标库地址:gitlab@gitlab.xxx.com/newgroups:newtest.com.git
1、克隆源版本库
在Shell执行如下命令:
git clone --bare git@github.xxx.com/groups:test.com.git
命令执行结果如下:
Initialized empty Git repository in /home/test/test.com.git/
remote: Counting objects: 8569, done.
remote: Compressing objects: 100% (2606/2606), done.
remote: Total 8569 (delta 6374), reused 7917 (delta 5793)
Receiving objects: 100% (8569/8569), 83.36 MiB | 9.57 MiB/s, done.
Resolving deltas: 100% (6374/6374), done.
2、设置目标仓库
1、将目标仓库master分支置为空,删除已经存在的文件(如果没有master,需先生成master分支,如添加README.md文件即可)
2、将master分支修改成UnProtected状态,也就是未保护状态。这个需要Git账号具有相应权限。修改位置项目->Settings->Repository->Protected Branches
点击Unprotect按钮即可取消master分支保护
3、将源Git镜像推送到目标库地址
进入克隆好的xxx.git文件夹中,执行命令:
git push --mirror gitlab@gitlab.xx.com:newgroups/newtest.com.git
命令执行结果如下:
Counting objects: 8569, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2025/2025), done.
Writing objects: 100% (8569/8569), 83.36 MiB | 28.47 MiB/s, done.
Total 8569 (delta 6374), reused 8569 (delta 6374)
To gitlab@github.meizu.com:private/ba.meizu.com.git
+ 5269023...ec9c11e master -> master (forced update)
* [new branch] feature/eco -> feature/eco
* [new branch] origin/feature/eco -> origin/feature/eco
如上所示,完整的Git仓库就迁移归来了,包括记录和各个分支。