Migrating a repository from Azure DevOps to GitHub using the command line

GitHub Feb 8, 2022

We've recently made a decision to move our source control, project management and build functions away from Azure DevOps and over to GitHub. The reasons behind the decision will be covered in another blog post but the first step in the process has been to migrate our Git repositories from Azure DevOps over to GitHub.

When it comes to migrating our repository there are two ways we can approach the task:

  1. We can use the import tool on the GitHub website OR
  2. We can use the command line

We covered how to use the import tool on the GitHub website in a previous post so this post will focus on the command line.

Creating our new repository on GitHub

The first thing we need to do is to set up a brand new repository on GitHub to house our migrated code. We can create a new repo on the GitHub website but this post is about the command line. I've previously posted about creating a new repository using the GitHub CLI so that's the approach we'll take here.

Assuming the GitHub CLI is already installed and authenticated we need to fire up our favourite terminal and issue the following command:

gh repo create sidebytes/migration-test --private

The CLI is going to need some extra information so it'll launch into it's interactive mode and there few important things to note:

  1. if you need a public or internal repository you can substitute the --private argument for --public or --internal
  2. we're not creating a .gitignore or a licence file as our repository likely already already has them if they are required
  3. we don't need a local clone of the repository we're creating on GitHub

Now we've created our repo on GitHub we need to grab it's clone URL. Unfortunately at the time of writing there doesn't seem to be any way to get this through the CLI so we'll have to get it from the GitHub website, the good news is we can use the CLI to launch into the repository on the web:

gh repo view Sidebytes/migration-test --web

The CLI will open a browser window and take you straight to the repo. At the top of the page is the clone URL for the new repository. We'll need this URL when we go to push our code into GitHub.

Migrating to GitHub

Personally, even if I have a clone of the Azure DevOps repo locally I like to use a clean "bear" clone to do the migration, ensuring we have the most up to date code from DevOps. After we've completed the migration we can re-point any local clones to GitHub which is covered later on in this post.

A "bear" clone of a Git repository creates a full copy of the data but doesn't create a working directory for editing files so it's ideal for this purpose.

Finding our Azure DevOps clone URL

To take the clone we first need to find the clone URL from DevOps. Keeping with our command line theme and assuming you have a local clone of the repo you can navigate to the repo in a terminal and type the following command:

git remote -v

As you can see in the screenshot above the fetch and push URL's are displayed, in this case we're interested in the fetch URL so let's take a note of it.

If you don't have a local clone of the repository you'll need to head to the Azure DevOps website. When you navigate to the repo you'll find the clone URL by clicking on the Clone button at the top right of your repository.

When you click the button it'll bring up the Clone Repository window which shows the HTTPS URL of your repository at the very top.

Cloning the repository and pushing it to GitHub

Now we have the clone URL's of our new GitHub and existing Azure DevOps repos we can begin the migration process.

We're going to create a temporary directory called migration-clones to store all my "bear" clones in, at the end of the migration the directory can be deleted. We'll do that in the command line now and navigate into the new directory:

md migration-clones && cd migration-clones

I'll then take the bare clone using the URL we obtained from Azure DevOps above:

git clone --bare https://organisation@dev.azure.com/organisation/Migration Test/_git/migration-test

Next we'll navigate into the cloned repo and push it to GitHub using the GitHub clone URL we obtained way back at the start of this post:

cd migration-test.git && git push --mirror https://github.com/Sidebytes/migration-test.git

Now if we look at GitHub our code should have been migrated and when we're ready we can clean up our temporary folders(s).

Re-pointing local clones to GitHub

Although our repository has now been migrated to GitHub any clones on our local machine are still pointing to Azure DevOps which is no longer correct.

We could simply delete any local clones of the repository and clone them fresh from GitHub but that seems pretty time consuming so instead we'll re-point our local clone from Azure DevOps to GitHub.

Using git remote -v from the command line will show the current origin for any repository. As you can see for our migration-test repository this is still pointing to Azure DevOps.

To update the URL of a remote we can use the git remote set-url command passing in the name of the remote we want to update, origin in this case, and the new remote URL which will be the GitHub clone URL for your repository. Altogether the command will look like this:

git remote set-url origin https://github.com/sidebytes/migration-test.git

If we run git remote -v again we should see the updated remote URL's and any changes we push will go to GitHub rather than Azure DevOps.

Remember to also update any CI/CD pipelines or other tools which access your repository.

Wrapping up

In this post we showed how to migrate a repository from Azure DevOps to GitHub using the command line.

Thinking of migrating from Azure DevOps to GitHub?

We can assist in all aspects of your migration from business case through to implantation. Please don't hesitate to get in touch at hello@sidebytes.com.


Andy Macdonald

Andy has been involved in the software industry for over 20 years as a developer, architect and team leader.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.