Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save royashbrook/cec9fb3c96f38f0b43bb4fd1f7697dbc to your computer and use it in GitHub Desktop.
Save royashbrook/cec9fb3c96f38f0b43bb4fd1f7697dbc to your computer and use it in GitHub Desktop.
Migrate git repos from Azure Devops to github
# how i moved all of my git repos from azure devops to github
# prereqs
# azure cli - https://docs.microsoft.com/en-us/cli/azure
# github cli - https://cli.github.com/
# note: you can get away with neither of these if you want
# to manually make some lists and repos yourself
# we'll be creating local clones of everything to work with
# i'm going to use an empty directory i already have created c:\gh
# let's switch over there before we start...
cd \gh
# login
az login
# get a list of projects, so we can get all the repos
$projects = (az devops project list | convertfrom-json).value
# get repos from each project
$repos = foreach($project in $projects){
az repos list --project $project.name |
convertfrom-json | %{$_}
}
#output a sorted version of this
$repos | sort weburl | select weburl,name | epcsv repos.csv
#cleanup repos.csv to taste, i had a lot of archived items to skip
# get a wip list of repos
$wiprepos = ipcsv repos.csv
# clone each repo using weburl, this may take a few moments
$wiprepos.weburl | %{git clone $_}
#in my case, some of my repos were using master for the default branch
# i have this changed for some and on github i am already using main
# but a lot of these are old repos and are using master and i wanted
# to swap it, so i'll go ahead and do that here first.
# i suppose this is a mini 'how-to' for switching all of your repos on
# on devops to main from master
$wiprepos.name | %{
#switch to the current repo
cd $_
#get the current branch
# this should be the default since we just cloned all of these
# that means if master is default, we'll be in it currently
$cb = git branch --show-current
#if our current branch is master, convert to main and update az repos
# you may want to echo the following commands, i have already copy
# and pasted these a bunch of times previously while changing some of these
if($cb -eq 'master'){
git branch -m master main
git push -u origin main
git push origin --delete master
az repos update --repository (split-path .\ -leaf) --default-branch main
}
#switch back to our wip dir
cd \gh
}
#now that our branches and default branch are done, it's time to setup our github repos
$org = 'yourgithuborg' # you can leave out org and it will just create them in your user
$wiprepos.name | %{
#switch to the current repo
cd $_
# remove old origin, create a repo, reassociate and push
git remote rm origin
gh repo create $org/$_ --private --confirm
git push --set-upstream origin main
#switch back to our wip dir
cd \gh
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment