将一个组织存储库中的所有分支复制或同步到具有相同存储库的另一个组织

问题描述 投票:0回答:1

我已经编写了 powershell 脚本,该脚本会将存储库从一个组织同步或复制到另一个组织,但目前此脚本仅同步主分支,而不同步所有分支。

以下是代码

<#
.SYNOPSIS
    Sync the Repo one from Org to another Org.
 
.DESCRIPTION
    Repo or Detla 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) {
    # Get all files in the folder
    $files = Get-ChildItem -Path $Path
    # Loop through each file and delete it
    foreach ($file in $files) {
        Remove-Item $file.FullName -Force
        Write-Output "Deleted: $($file.Name)"
        }
}
 
#Source URL to fetch the Repo
$Sourceurl = "https://$(SourceOrg):$(PAT)@dev.azure.com/$(SourceOrg)/$(URLSourceProject)/_git/$(API_Repo)"
 
#Destination URL to push the Repo
$DestinationURL = "https://$(DestinationOrg):$(PAT)@dev.azure.com/$(DestinationOrg)/$(DestinationProject)/_git/$(API_Repo)"
 
#Set Location for current folder
Set-Location "$(System.DefaultWorkingDirectory)"
Write-Output "***** Cloning the Source URL*****"
git clone $Sourceurl
 
#Set Location to get the latest repo from source
Write-Output "Source Project: $(API_Repo)"
Set-Location "$(System.DefaultWorkingDirectory)\$(API_Repo)\"
Write-Output "*****Git removing remote origin****"
 
#Git Commands to fetch and push the code
git remote rm origin
 
Write-Output "*****Git remote add****"
git remote add --mirror=fetch origin $DestinationURL
 
Write-Output "*****Git fetch origin****"
git fetch $sourceURL
 
Write-Output "*****Git push to Global Repos****"
git push origin --all -f

我已将行从 git fetch $sourceURL 修改为 git fetch origin 以获取存储库中的所有分支,但是当我运行管道时,我看到此警告或错误。

致命:拒绝获取在“/home/vsts/work/1/s/Polydone.Api”处签出的分支“refs/heads/master”

git synchronization repository clone git-clone
1个回答
0
投票

1.克隆源存储库: 首先,使用 Git 将源存储库从原始组织克隆到本地计算机。将 SOURCE_ORG 替换为源组织的名称,将 REPO_NAME 替换为存储库名称: git clone --bare https://github.com/SOURCE_ORG/REPO_NAME.git

2.导航到克隆存储库: 输入克隆的存储库目录: cd REPO_NAME.git

3.镜像推送到目标仓库: 将存储库镜像推送到目标组织的存储库。将 DEST_ORG 替换为目标组织的名称: git push --mirror https://github.com/DEST_ORG/REPO_NAME.git

4.更新远程URL: 更新克隆存储库的远程 URL 以指向目标组织的存储库: git Remote set-url origin https://github.com/DEST_ORG/REPO_NAME.git

5.获取所有分支: 从源存储库中获取所有分支: git fetch --全部

6.推送所有分支: 将所有分支推送到目标存储库: git push --全部

7.推送标签(可选): 如果源存储库中有标签并希望将其推送到目标存储库,请使用以下命令: git push --标签

这些步骤将克隆源存储库,将其推送到目标存储库,更新远程 URL,获取所有分支,并将所有分支推送到目标存储库。根据您的特定场景的需要调整组织名称(SOURCE_ORG 和 DEST_ORG)和存储库名称 (REPO_NAME)。

© www.soinside.com 2019 - 2024. All rights reserved.