git命令上传本地项目到github上面的时候,有些大目录会因为平台的限制而上传不成功,只能使用脚本自动切割成一定大小的文件来分批上传了,这样比较高效。 # ========================================== # 【终极补漏脚本】每次严格限制 40 个文件、24MB 以内 # ========================================== $maxFileCount = 40 $maxBatchSize = 24MB # 留 1MB 安全余量 # ------------------------------------------ # 定向处理目录函数(不影响其他任何已上传的文件) # ------------------------------------------ function Push-Directory-In-Batches ($targetDir) { if (-not (Test-Path $targetDir)) { Write-Host "目录 $targetDir 不存在,跳过..." -ForegroundColor Gray return } Write-Host "🔍 正在扫描 $targetDir 目录下的文件..." -ForegroundColor Cyan $files = Get-ChildItem -Path $targetDir -Recurse -File | ForEach-Object { $_.FullName.Replace((Get-Location).Path + "\", "").Replace("\", "/") } if ($files.Count -eq 0) { Write-Host "目录 $targetDir 为空,无需上传。" -ForegroundColor Gray return } $currentBatch = New-Object System.Collections.Generic.List[String] $currentSize = 0 $batchCount = 1 $fileCountInBatch = 0 Write-Host "🎯 开始分批搬运 $targetDir,文件总数: $($files.Count)" -ForegroundColor Green foreach ($file in $files) { $fileSize = (Get-Item $file).Length # 严格限制:满40个文件或即将超24MB就打包发送一次 if (($fileCountInBatch -ge $maxFileCount) -or (($currentSize + $fileSize) -gt $maxBatchSize)) { if ($currentBatch.Count -gt 0) { Write-Host "------------------------------------------" -ForegroundColor Yellow Write-Host "📦 正在推送 $targetDir 批次 #$batchCount ($fileCountInBatch 个文件, [$( [Math]::Round($currentSize/1MB, 2) ) MB])" -ForegroundColor Yellow # 强行勾选并提交这 40 个文件 foreach ($batchFile in $currentBatch) { git add -f $batchFile } git commit -m "补传同步: $targetDir #$batchCount" # 叠加式推送(绝不使用 -f,只在云端追加文件,保证不破坏已有的其他目录) do { git push origin main $success = $? if (-not $success) { Write-Host "⚠️ 网络波动,3秒后自动重试..." -ForegroundColor DarkYellow Start-Sleep -Seconds 3 } } while (-not $success) $currentBatch.Clear() $currentSize = 0 $fileCountInBatch = 0 $batchCount++ } } $currentBatch.Add($file) $currentSize += $fileSize $fileCountInBatch++ } # 收尾该目录的最后一批 if ($currentBatch.Count -gt 0) { Write-Host "------------------------------------------" -ForegroundColor Yellow Write-Host "📦 正在推送 $targetDir 最终收尾批次 ($fileCountInBatch 个文件)" -ForegroundColor Yellow foreach ($batchFile in $currentBatch) { git add -f $batchFile } git commit -m "补传同步: $targetDir 最终收尾" git push origin main } Write-Host "✅ $targetDir 目录已全部成功追加到云端!" -ForegroundColor Green } # ------------------------------------------ # 执行定向补传(依次吃掉 uploads 和 node_modules) # ------------------------------------------ Push-Directory-In-Batches "uploads" Push-Directory-In-Batches "node_modules" Write-Host "==========================================" -ForegroundColor Green Write-Host "🎉 【大功告成】所有目录和海量大文件已整整齐齐地在 GitHub 云端全员团聚!" -ForegroundColor Green 未经允许不得转载:泥人传说 » git命令通过切割分批次的补传大目录的脚本 gitgit命令git脚本上传大文件 分享到: 赞(0)