git批量克隆仓库代码以及批量上传
批量克隆仓库脚本:
PowerShell 全选
# 仓库列表文件路径
$repoListFile = "repos.txt"
# 检查仓库列表文件是否存在
if (-not (Test-Path $repoListFile)) {
Write-Host "仓库列表文件 ($repoListFile) 不存在,请检查文件路径。"
exit
}
# 读取仓库 URL 列表
$repos = Get-Content $repoListFile
foreach ($repoUrl in $repos) {
# 如果 URL 为空,跳过
if ($repoUrl.Trim() -eq "") {
continue
}
# 提取仓库名作为备份目录(假设仓库 URL 格式是 GitHub 或类似格式)
$repoName = $repoUrl.Split("/")[-1].Replace(".git", "") # 提取仓库名
$backupDir = "$repoName" # 定义备份路径
Write-Host "开始克隆仓库: $repoUrl 到 $backupDir"
# 克隆仓库,包括所有分支、标签和工作区文件
git clone --recursive $repoUrl
# 进入克隆的仓库目录
cd $backupDir
# 确保获取所有分支和标签
git fetch --all --tags
# 提示用户确保新仓库已部署好
#Write-Host "请确保新仓库已部署并可以访问,接下来将仓库内容推送到新的仓库 URL。"
# 拼接新的仓库 URL
#$newRepoUrl = "$newServerUrl$repoName.git"
#Write-Host "新仓库的 URL: $newRepoUrl"
# 删除旧的远程仓库 URL 并设置新仓库的 URL
#git remote set-url origin $newRepoUrl
# 推送所有分支和标签到新的仓库
#Write-Host "开始推送到新仓库..."
#git push --mirror $newRepoUrl
#Write-Host "$repoName 仓库已成功推送到新仓库!"
cd ..
}
Write-Host "所有仓库迁移完成!"
批量推送仓库脚本:
PowerShell 全选
# 仓库列表文件路径
$repoListFile = "repos.txt"
# 检查仓库列表文件是否存在
if (-not (Test-Path $repoListFile)) {
Write-Host "仓库列表文件 ($repoListFile) 不存在,请检查文件路径。"
exit
}
# 读取仓库 URL 列表
$repos = Get-Content $repoListFile
foreach ($repoUrl in $repos) {
# 如果 URL 为空,跳过
if ($repoUrl.Trim() -eq "") {
continue
}
# 提取仓库名作为备份目录(假设仓库 URL 格式是 GitHub 或类似格式)
$repoName = $repoUrl.Split("/")[-1].Replace(".git", "") # 提取仓库名
$backupDir = "$repoName" # 定义备份路径
# 进入克隆的仓库目录
cd $backupDir
# 提示用户确保新仓库已部署好
#Write-Host "请确保新仓库已部署并可以访问,接下来将仓库内容推送到新的仓库 URL。"
# 拼接新的仓库 URL
$newRepoUrl = "ssh://git@gitlab.**.com/mycode/$repoName.git"
Write-Host "新仓库的 URL: $newRepoUrl"
# 删除旧的远程仓库 URL 并设置新仓库的 URL
git remote set-url origin $newRepoUrl
# 推送所有分支和标签到新的仓库
Write-Host "开始推送到新仓库..."
git push --mirror $newRepoUrl
Write-Host "$repoName 仓库已成功推送到新仓库!"
cd ..
}
Write-Host "所有仓库迁移完成!"
repos.txt示例:
C# 全选
http://127.0.0.1/MyCode/Project1.git
http://127.0.0.1/MyCode/Project2.git
http://127.0.0.1/MyCode/Project3.git
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生