Convert Bitbucket Mercurial repository to Git. Maintain branches and history. Online solution

The only way I found to convert a Mercurial repo to Git, while keeping all the branches, was to use GitHub’s importer. This was also the easiest since it’s all done online. Nothing local to install, no command line stuff.

Main idea

Use Github’s importer to convert Bitbucket Mercurial repo to GitHub Git repo. Then import that back into Bitbucket.

The steps

In Bitbucket:

Rename the Mercurial repository that you want to convert (if you want the new Git repo to have the same name as your Mercurial repo did). You do this in Repository Settings. For example, name it projectname_mercurial.

Grab the Clone URL of the repository. (On the repo’s main page, click Clone and grab just the URL, like https://[email protected]/username/projectname )

In Github:

If you don’t already have an account, create one https://github.com

Use the GitHub importer https://github.com/new/import

  • Enter the Clone URL from Bitbucket.
  • Give it a name. It doesn’t really matter what.
  • Set it to private. Public is default!

NOTE: You can add your password to the URL directly, which will then avoid the need to enter your credentials in the steps below (thanks to @keith-turkowski comment below). So change your bitbucket clone url by adding :password after username, like this: https://username:[email protected]/username/projectname

2022 NOTE. Don’t know if the above password url trick still works. For the credential method below, I first had to create an App Password in Bitbucket where I chose “read” (and only read) for every option. Use this for the password when Github asks below. Also, username is just your name, not your email.

Begin Import in Github

WAIT! Do not close the page when it says that you can (We’ll email you…). Wait for it to ask you for the repo credentials. This can take several minutes. Then enter the credentials. (Assuming it requires credentials.)

WAIT AGAIN! It’s again going to ask for your repo credentials. At least it did for me, every time. (See note above on adding your password directly to URL so it does not ask you.)

Now you wait until it’s done. You will get an email.

When it’s done, grab the GitHub project URL. It’s just the webpage URL when you are on the GitHub project page.

In Bitbucket:

Create a new repository using the “+” > Import > Repository menu option.

  • Enter the GitHub project URL
  • Check “Requires authentication” Enter your GitHub credentials.
  • Enter the new Git repository name (For example, the name of your Mercurial project before you renamed it.)
  • Keep Access level as Private.

In Github:

Delete the imported project:
Project’s Settings tab > Delete this repository

Leave a Comment