从Azure DevOps迁移至GitHub时如何忽略大文件?
解决Azure DevOps Git镜像到GitHub的大文件限制问题
要将Azure DevOps Git仓库镜像到GitHub,但GitHub限制单文件大小不超过100MB,当前脚本因历史提交中存在157.5MB的my-file.exe报错。需求是不使用Git LFS、不修改原仓库历史,仅在镜像过程中排除该大文件。
以下是修改后的完整PowerShell任务脚本:
task: PowerShell@2 inputs: targetType: 'inline' script: | Write-Host ' - - - - - - - - - - - - - - - - - - - - - - - - -' Write-Host ' mirror Azure DevOps repo changes to GitHub repo' Write-Host ' - - - - - - - - - - - - - - - - - - - - - - - - - ' $stageDir = '$(Build.SourcesDirectory)' | Split-Path $githubDir = $stageDir + "\gitHub" $destination = $githubDir + "\$(devOpsRepoName).git" $sourceURL = 'https://$(devops.pat)@dev.azure.com/$(devOpsProjectPath)/_git/$(devOpsRepoName)' $destURL = 'https://$(github.pat)@github.com/organization/$(gitHubRepoName).git' # 清理已存在的临时目录 if((Test-Path -path $githubDir)) { Remove-Item -Path $githubDir -Recurse -force } # 创建临时目录并克隆镜像仓库 New-Item -ItemType directory -Path $githubDir Set-Location $githubDir Write-Output '*****Clone mirror repo****' git clone --mirror $sourceURL Set-Location $destination Write-Output '*****Remove large file from all commit history****' # 自托管代理需取消注释以下命令安装git-filter-repo,微软托管代理已预装 # choco install git-filter-repo -y git filter-repo --path my-file.exe --invert-paths Write-Output '*****Configure GitHub remote****' git remote rm secondary git remote add --mirror=fetch secondary $destURL Write-Output '*****Push cleaned repo to GitHub****' git push secondary --all Write-Output '**Azure Devops repo synced with Github repo (large file excluded)**' # 清理临时目录 Set-Location $stageDir Remove-Item -Path $githubDir -Recurse -force
关键修改说明
- 移除历史大文件:使用
git filter-repo(Git官方推荐替代git filter-branch的工具),通过--path my-file.exe --invert-paths参数彻底删除该文件在所有历史提交中的痕迹,不会影响原Azure DevOps仓库。如果有多个大文件,可重复添加--path 文件名参数。 - 代理适配:微软托管的Azure DevOps代理已预装
git-filter-repo,若使用自托管代理,取消注释choco install命令即可完成安装。 - 简化逻辑:删除了原脚本中冗余的目录判断,直接清理后重新创建临时目录,避免分支残留导致的异常。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

