You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量迁移多个GitLab仓库至GitHub:如何一次性操作(替代手动)

批量将GitLab仓库迁移至GitHub的方法

方法一:用Python脚本结合API自动迁移

这种方式能自动完成仓库创建、代码推送全流程,适合批量处理大量仓库。

  1. 先准备必要的API令牌

    • GitLab:登录后进入「Settings > Access Tokens」,生成一个具备read_repository和api权限的令牌
    • GitHub:登录后进入「Settings > Developer settings > Personal access tokens」,生成一个具备repo权限的令牌
  2. 运行以下Python脚本(记得替换脚本里的占位变量)

import requests
import subprocess
import os

# 替换成你的信息
GITLAB_TOKEN = "your_gitlab_token"
GITHUB_TOKEN = "your_github_token"
GITLAB_USERNAME = "your_gitlab_username"
GITHUB_USERNAME = "your_github_username"
GITLAB_API_URL = "https://gitlab.com/api/v4"

# 获取GitLab所有仓库
def get_gitlab_repos():
    headers = {"PRIVATE-TOKEN": GITLAB_TOKEN}
    repos = []
    page = 1
    while True:
        response = requests.get(f"{GITLAB_API_URL}/users/{GITLAB_USERNAME}/projects?page={page}&per_page=100", headers=headers)
        if not response.json():
            break
        repos.extend(response.json())
        page += 1
    return repos

# 创建GitHub仓库并推送代码
def migrate_repo(repo):
    repo_name = repo["name"]
    gitlab_repo_url = repo["ssh_url_to_repo"]
    
    # 创建GitHub仓库
    github_api_url = "https://api.github.com/user/repos"
    headers = {"Authorization": f"token {GITHUB_TOKEN}"}
    data = {"name": repo_name, "private": repo["visibility"] == "private"}
    requests.post(github_api_url, json=data, headers=headers)
    
    # 克隆GitLab仓库并推送到GitHub
    temp_dir = f"./temp_{repo_name}"
    subprocess.run(["git", "clone", "--mirror", gitlab_repo_url, temp_dir], check=True)
    os.chdir(temp_dir)
    subprocess.run(["git", "remote", "add", "github", f"git@github.com:{GITHUB_USERNAME}/{repo_name}.git"], check=True)
    subprocess.run(["git", "push", "--mirror", "github"], check=True)
    os.chdir("..")
    subprocess.run(["rm", "-rf", temp_dir], check=True)

if __name__ == "__main__":
    repos = get_gitlab_repos()
    for repo in repos:
        print(f"Migrating {repo['name']}...")
        migrate_repo(repo)
    print("All repos migrated successfully!")

方法二:用Git命令批量手动迁移

如果不想写脚本,可通过命令行工具批量处理:

  1. 导出GitLab仓库列表
    用curl调用GitLab API获取所有仓库的SSH链接,保存到文件:

    curl --header "PRIVATE-TOKEN: your_gitlab_token" "https://gitlab.com/api/v4/users/your_gitlab_username/projects?per_page=100" | jq -r '.[].ssh_url_to_repo' > gitlab_repos.txt
    

    (需提前安装jq工具解析JSON)

  2. 循环处理每个仓库
    创建bash脚本(比如migrate_repos.sh):

    #!/bin/bash
    GITHUB_USERNAME="your_github_username"
    
    while read repo_url; do
        repo_name=$(basename -s .git "$repo_url")
        echo "Processing $repo_name..."
    
        # 克隆镜像仓库
        git clone --mirror "$repo_url"
        cd "$repo_name.git"
    
        # 创建GitHub仓库(需提前安装GitHub CLI并登录)
        gh repo create "$GITHUB_USERNAME/$repo_name" --private=$(echo "$repo_url" | grep -q private && echo true || echo false) --source=. --remote=github
    
        # 推送所有内容
        git push --mirror github
        cd ..
        rm -rf "$repo_name.git"
    done < gitlab_repos.txt
    

    若不想用GitHub CLI,也可手动在GitHub批量创建仓库后,直接执行推送步骤。

注意事项

  • 迁移前确认仓库大小,避免超出GitHub存储限制
  • 私有仓库迁移后,检查GitHub仓库的权限设置是否正确
  • 迁移完成后,建议逐个验证分支、标签和提交记录是否完整

内容的提问来源于stack exchange,提问作者LearnerJS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:54:22