Z
ZengLiangYi
Back
开源2026-03-022 min read

Git 仓库批量迁移:从 Gitee/GitLab 到 GitHub 的自动化方案

一键批量迁移数十个仓库,保留完整 commit 历史,支持双向迁移。

GitCLIAutomationShell

为什么需要迁移

国内开发者经常同时使用 Gitee 和 GitHub。当你决定把主力平台切换到 GitHub 时,面临的问题是:几十个仓库,手动迁移太痛苦

我需要一个脚本:

  1. 批量读取源平台的所有仓库
  2. 在 GitHub 上创建对应仓库
  3. 推送完整历史(所有分支和标签)
  4. 支持私有仓库

实现方案

核心流程

# 1. 获取源平台仓库列表
repos=$(curl -s "https://gitee.com/api/v5/user/repos?access_token=$TOKEN&per_page=100")

# 2. 遍历每个仓库
for repo in $repos; do
  # 3. 克隆镜像
  git clone --mirror "$repo_url" "$temp_dir"

  # 4. 在 GitHub 创建仓库
  gh repo create "$repo_name" --private

  # 5. 推送镜像
  cd "$temp_dir"
  git remote set-url origin "https://github.com/$GITHUB_USER/$repo_name.git"
  git push --mirror
done

关键细节

镜像克隆 vs 普通克隆

git clone --mirror 会克隆所有引用,包括所有分支、标签、notes 等。这是确保完整迁移的关键。

# 普通克隆:只有默认分支
git clone https://gitee.com/user/repo.git

# 镜像克隆:所有分支、标签、引用
git clone --mirror https://gitee.com/user/repo.git

git push --mirror

对应的推送命令是 git push --mirror,它会推送所有本地引用到远程。

使用方式

# 设置环境变量
export GITEE_TOKEN="your_gitee_token"
export GITHUB_TOKEN="your_github_token"
export GITHUB_USER="ZengLiangYi"

# 执行迁移
./migrate.sh

总结

30 行 Shell 脚本,解决了手动迁移几十个仓库的痛苦。关键是 --mirror 参数保证了完整性。

项目地址:GitHub