跨组织Azure DevOps仓库全分支同步问题:现有PowerShell脚本仅同步Master分支
跨组织Azure DevOps仓库全分支同步问题:现有PowerShell脚本仅同步Master分支
我来帮你调整这个PowerShell脚本,让它能够同步源仓库的所有分支到目标组织的仓库里。原脚本的核心问题是只处理了master分支,没有对远程所有分支做拉取和推送操作,下面是修改后的完整脚本,我会标注关键改动点:
<# .SYNOPSIS Sync the Repo one from Org to another Org. .DESCRIPTION Repo or Delta sync of entire repo from Org to another org using Powershell script. #> #Clear Console Clear-Host # Delete the Old Destination Folder $Path = "$(System.DefaultWorkingDirectory)\$(API_Repo)\" if (Test-Path $Path -PathType Container) { # 直接删除整个文件夹更高效,避免逐个删除文件 Remove-Item -Path $Path -Recurse -Force Write-Output "Deleted existing destination folder: $Path" } #Source URL to fetch the Repo $Sourceurl = "https://$(SourceOrg):$(PAT)@dev.azure.com/$(SourceOrg)/$(URLSourceProject)/_git/$(API_Repo)" # 新增目标仓库URL变量,需替换为你的目标组织信息 $DestUrl = "https://$(DestOrg):$(PAT)@dev.azure.com/$(DestOrg)/$(URLDestProject)/_git/$(API_Repo)" # 1. 以镜像模式克隆源仓库,拉取所有分支、标签和完整提交历史 git clone --mirror $Sourceurl $Path # 进入克隆后的仓库目录 Set-Location -Path $Path # 2. 将目标仓库设置为远程推送地址 git remote add destination $DestUrl # 3. 以镜像模式推送到目标仓库,同步所有分支、标签内容 git push --mirror destination # 回到初始工作目录 Set-Location -Path $env:SystemDefaultWorkingDirectory Write-Output "All branches and tags have been synced successfully!"
关键改动说明:
- 使用
git clone --mirror:这个参数会克隆源仓库的所有远程分支、标签和完整提交历史,而非仅默认分支,是跨组织全量同步的可靠方式 - 优化了旧文件夹删除逻辑:直接删除整个文件夹比逐个删除文件更简洁高效
- 使用
git push --mirror:会把本地镜像仓库的所有内容同步到目标仓库,确保源仓库的分支、标签完全复刻到目标端 - 新增目标仓库URL变量:你需要把
$(DestOrg)、$(URLDestProject)替换为实际的目标组织和项目名称
如果你的场景需要增量同步(而非每次全量克隆),也可以调整逻辑:先拉取源仓库的所有分支更新,再推送到目标仓库,但镜像模式对于跨组织同步来说,能最大程度保证两边仓库内容完全一致。
备注:内容来源于stack exchange,提问作者chandu
相关产品推荐
相关产品推荐

