loading...
Mega Therion AS

Moving internal git repo to devops or github using PowerShell

alexanderviken profile image Alexander Viken ・2 min read

Alt Text
I just started working with a new customer, to help them modernize their software development. One of the first tasks we decided on was to discontinue their use of Atlassian Stash for managing their repositories, and move to Azure DevOps so we could use both repos and pipelines.

Our first issue was that the Stash server didn't have an internet reachable endpoint, it was only available on the internal network. The second was that over the years with somewhat unplanned git usage there was a huge amount of branches they needed and wanted for historic purposes, so the only option was to move everything, all branches.

I didn't find any good tools to automate this, to move everyting, of the box so I decided to write a couple of powershell functions to help me.
First, a function for checkout of the stash branches to my workstation, then a function to push them to an Azure Repo.

I had already cloned the repository from Stash so it was set up with the stash repo url as origin so the first function loops through all branches and checks out all remotes/origin branches.

function CheckoutAllBranches() 
{

$branches = git branch --all

    foreach($branch in $branches)
    {
        if ($branch -like "*remotes/*") 
        {
            $branchName = $branch -replace "remotes/origin/" -replace ""
            $gitCheckout = "/c git checkout " + $branchName
            Start-Process cmd -Argument $gitCheckout -Wait
            Write-Host('checkedout branch: '+ $branchName )
       }
    }
}
Enter fullscreen mode Exit fullscreen mode

The second function loops through all the local branches and pushes them to the Azure Repo.

function PushToDevops() 
{
    $makeRemote = git remote add devops [url_to_azure_repo] 
    $branches = git branch
    foreach($branch in $branches)
    {
        $gitPush = "/c git push devops " + $branch 
        Start-Process cmd -Argument $gitPush -Wait
        Write-Host('pushed branch: '+ $branchName )
    }
}
Enter fullscreen mode Exit fullscreen mode

I saved them to a ps1 file and executed the ps1 script file. I then cd'ed into the folder where I cloned the stash repo and run the checkout function:

d:\repos\myrepo\>CheckoutAllBranches
Enter fullscreen mode Exit fullscreen mode

Depending on how many branches you have - just wait. When It's done run the second function

d:\repos\myrepo\>PushToDevops
Enter fullscreen mode Exit fullscreen mode

Pretty simple, but I haven't found any better tool that will let med move a big repository from private on-prem to external repo.

Discussion

pic
Editor guide