Script to copy all the updates on gitlab repository to azure devops repository


In the organisation, we are in the process of migration from gitlab to azure. There are some repositories which has a lot of historical commits/branches/tags that we need to mirror to azure-devops. I would need some suggestions if we could write a script to do the same? Schedule some automatism to do so? Could anyone give me some suggestions/Examples to start off?


You can use REST API to migrate from github to azure and the reference document is here:Import Request-Create-REST API(Azure DevOps Git)

But if your repositories are private, it is vital for you to create an "other git" service connection first, then you could use the Rest API to import the Github Private Repo to New Repo.

1、You could use Rest API to create it. And the document is here:

For example:



Request Body

    "authorization":{"scheme":"UsernamePassword","parameters":{"username":"{User name}","password":"{github access token }"}},
    "serviceEndpointProjectReferences":[{"description":"","name":"{Service connection name}","projectReference":{"id":"{Project Id}","name":"{Project Name}"}}],
    "url":"{Target Git URL}",

You can test in postman:
Create Endpoint with REST API in postman

After send creating endpoint API, it will create endpoint successfully in your Azure DevOps.

Note: how to get github access token:

Path : Settings->Develop Settings->Personal access tokens

github Personal Access Token

2 Then you could get the ServiceEndPointId in the step 1, and you could use it in the Import Repo Rest API.
For example:


Post{Organization Name}/{Project Name}/_apis/git/repositories/{Repo Name}/importRequests?api-version=5.0-preview.1 

Request Body

  "parameters": {
    "gitSource": {
      "url": "Git URL"
    "serviceEndpointId": "{Service EndPoint Id}",
    "deleteServiceEndpointAfterImportIsDone": false

You can test in postman:
Create import repository with REST API in postman

3 Also ,the below script is an power shell example :

  [String]$Org = "your organization name"
  [String]$project = "your project name"
  [String]$PAT="your PAT "
  [String]$Repo="your Repo name"
  [String]$serviceEndpointId="your serviceEndpointId"

$url =$Org+"/"+"$project"+"/_apis/git/repositories/"+$Repo+"/importRequests?api-version=6.1-preview.1" 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT )))

 $body = @{
   "parameters" = @{
        "gitSource" [email protected]{
         # the source git repository to import and remember to replace with your correct url

          "url" =
       "serviceEndpointId" = ]$serviceEndpointId
       "deleteServiceEndpointAfterImportIsDone" = false

 $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT )))

 $result = Invoke-RestMethod -Method 'Post' -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body ($body|ConvertTo-Json)   -ContentType "application/json"
 $result  | ConvertTo-Json

After running the script in power shell, you can get the below response info in json that means you migrate from github to azure with REST API successfully:
script in power shell

Answered By – Dou Xu-MSFT

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published