Git 工程迁移指南
Git 工程代码迁移完整指南
将 Git 代码仓库从一个位置迁移到另一个位置(如跨平台迁移或改变存储路径)需要保存完整历史记录和分支结构,以下是专业高效的迁移流程:
📌 核心迁移流程(完整历史保留)
1. 克隆源仓库(包含所有分支和历史)
git clone --mirror <source-repo-url>
cd <repo-name>.git
2. 添加目标仓库作为新的远程地址
git remote add target <target-repo-url>
3. 推送完整历史到目标仓库
git push target --mirror
💡 不同场景下的迁移方案
场景1:平台间迁移 (GitHub → GitLab)
GitHub → GitLab 完整迁移
git clone --mirror https://github.com/user/repo.git
cd repo.git
git remote add gitlab https://gitlab.com/user/new-repo.git
git push gitlab --mirror
场景2:迁移到新目录(保留所有分支)
本地仓库迁移到新路径
cd old-project
git clone --mirror file://$(pwd) ../new-project
cd ../new-project
git remote set-url origin <new-remote-url>
git push --mirror
场景3:迁移特定分支(非完整迁移)
仅迁移master和develop分支
git clone --single-branch --branch master <source-url>
git remote add target <target-url>
git push target master
git push target develop
🛠️ 迁移后验证(关键步骤)
检查分支完整性
git branch -a
git tag -l
验证提交历史
git log --oneline --graph --all
确认文件完整性
git checkout master
git fsck --full
🌟 高级迁移技巧
-
子模块迁移
git submodule update --init --recursive git push --recurse-submodules=on-demand target --all
-
大文件迁移优化
git lfs fetch --all git lfs push target --all
-
保存仓库配置
cp .git/config config.backup
🔍 常见问题解决
问题1:LFS对象迁移失败
强制重新上传LFS
git lfs push target --force --all
问题2:权限验证失败
使用SSH密钥替代HTTPS
git remote set-url target git@github.com:user/repo.git
问题3:迁移后分支不显示
更新远程引用
git remote update target --prune