前言
近期工作上计划将 GitLab 迁移到 GitHub,但光是腾出时间进行项目迁移就感觉很麻烦,也因此很多计划被耽搁着,像是构建 CI 自动化测试环境、导入 AI 代码审核。
幸好 GitLab 很早就有一项功能叫做 Mirroring 有三种方式可以同步 Git Repo 到其他地方,只需简单通过网页界面就能快速建立 GitLab Repo 同步的功能:
- Push:把 GitLab Repo 同步到其他地方 Repo
- Pull:把其他地方 Repo 同步到 GitLab
- Bidirectional:双向同步
除了 Push 服务免费外其余都要付费,这篇文章根据我的迁移经验说明如何建立 GitLab push GitHub 实现代码跨平台的迁移。
GitLab Push
准备好要被迁移的 GitLab Repo 与创建一个全新的 GitHub Repo。
- 到目标 GitLab Repo
- 侧栏 Settings > Repository
- 打开 Mirroring repositories > Add New
- 输入 Git repository URL。
The repository must be accessible over http://, https://, ssh:// or git://.When using the http:// or https:// protocols, please provide the exact URL to the repository. HTTP redirects will not be followed.Do not include the username in the URL, use the username field below if required: https://gitlab.company.com/group/project.git.When using the ssh:// protocol, please use the following format: ssh://[email protected]/group/project.git.The update action will time out after 180 minutes. For big repositories, use a clone/push combination.Pull mirrors will only create LFS objects if LFS is enabled for the project.Push mirrors will only sync LFS objects if LFS is enabled for the project.Push mirrors will not sync LFS objects over SSH.In case of pull mirroring, your user will be the author of all events in the activity feed that are the result of an update, like new branches being created or new commits being pushed to existing branches.会发现该栏位有很多描述,我没有耐心看完,也付出代价浪费许多时间 🫠。总之可以到「目标 GitHub Repo 的帐号」创建存取 Token(Settings > Developer Settings > Personal access tokens)有两种类型:
- Fine-grained(授权范围更精细到 Repo 层级,需要
contents读写权限) - classic(传统,需要
Repo权限)
最后假设 GitHub 用户名叫做 foobar,Repo 名称叫做 sync-demo
- Git repository URL
https://github.com/foobar/sync-demo.git
- Authentication method(Username and Password)
- Username
- foobar
- Password
- 先前创建的存取 Token
- Username
创建 Mirror 后就会看到它自动同步两边 Repo 的内容了,也可以手动触发。有点像现成的 CI,很方便很快。