Converting a Mercurial repository to Git (Windows)

After going through the pain of (re-)discovering how to convert a Mercurial repository into a Git repository on Windows, I thought I’d share how easy it really is. I’ve bounced back and forth between Mercurial and Git a few times – my current preference is Git, mainly because I like Git’s branching strategy a bit better – but really, they’re both excellent choices. I still find the best analogy for comparing them is that Git is MacGyver and Mercurial is James Bond.

You can find quite a few posts describing how to convert – but many of the steps mentioned in those guides are not needed if you have TortoiseHg installed, which most Windows Mercurial users do.

Prerequisites

As I already mentioned, this guide expects that you have TortoiseHg installed on your system.

For the actual conversion, we’re going to be using a Mercurial extension called hggit that enables Mercurial to push and pull from Git repositories. You can either clone the hggit repository on GitHub or grab a zipped version here.

What we need is the hggit folder from the clone or zip file – put this some place handy and remember the path.

Preparing the Git repository

In this guide we’re going to be pushing our repository to a local Git repository – so let’s create a bare repository – this way you’ll avoid Git complains about pushing to a non-bare repository.

Open a command prompt, create a directory for the new repository and from within the directory execute: git init –bare

That’s it – our Git repository is ready. Alternatively you could push directly to a Git repository on either GitHub, Bitbucket or other provider.

Enabling hggit in Mercurial

Now we need to let Mercurial know about the hggit extension. This is done by adding it to the .hgrc or mercurial.ini file in your home directory (for me that’d be c:\users\rasmuskl\mercurial.ini).

In the config file, find the [extensions] section – or add it at bottom if it’s not already there. Then add a reference to the hggit extension followed by the path of the hggit folder:

1
hggit = c:\path\to\hggit

Converting the repository

To convert the repository, simply open your command prompt, navigate to your Mercurial repository and do: hg push c:\path\to\bare\git\repository

And you’re done. You can now either clone the bare repository to a working directory – or push it to your GitHub or Bitbucket account.