前言
近期工作上計劃將 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,很方便很快。