如何迁移 GitHub 仓库到 GitLab?
如何迁移 GitHub 仓库到 GitLab?
一、基础迁移方法(保留完整历史)
1.在 GitLab 创建空仓库
1.登录 GitLab 并新建项目,选择「空白项目」,不要初始化 README 或 LICENSE 文件
2.复制新建仓库的 HTTPS/SSH 地址(如 https://gitlab.com/用户名/新仓库名.git)。
2.本地克隆 GitHub 仓库镜像
git clone --mirror https://github.com/用户名/旧仓库.git
cd 旧仓库.git # 进入克隆生成的隐藏目录
该命令会完整克隆所有分支、标签和提交历史
3.修改远程仓库地址
git remote set-url --push origin https://gitlab.com/用户名/新仓库.git
4.推送全部内容到 GitLab
git push --mirror
二、替代方案(手动迁移)
若只需迁移部分内容:
1.本地克隆 GitHub 仓库
git clone https://github.com/用户名/旧仓库.git
cd 旧仓库
2.关联 GitLab 远程仓库
git remote add gitlab https://gitlab.com/用户名/新仓库.git
3.选择性推送
git push gitlab master # 推送主分支
git push gitlab --all # 推送所有分支
git push gitlab --tags # 推送所有标签
三、注意事项
1.冲突处理
若 GitLab 仓库非空,需先清空或强制覆盖(git push -f)
2.验证迁移
git remote -v # 检查远程仓库地址
git log # 确认提交历史完整性
通过 GitLab 网页端核对分支和文件状态
四、常见报错
error: remote coms already exists
.
报错内容 尝试添加一个已经存在的远程仓库
五、解决方案
1.查看所有已配置的远程仓库
git remote -v
这个命令会列出所有远程仓库的名称和对应的 URL。检查你的远程仓库是否已经存在。
2.更新远程仓库URL(如果需要)
git remote set-url origin 新URL
这里origin是远程仓库的名称,新URL是你想要更新的远程仓库的URL
3. 删除并重新添加远程仓库
git remote remove origin
git remote add origin 新URL
4.提交代码
git push --mirror
或
git push gitlab master # 推送主分支
git push gitlab --all # 推送所有分支
git push gitlab --tags # 推送所有标签