Convert a Mercurial Repository to Git

Did the following on my Mac to successfully export a Mercurial repo to Git (with full branches):

mkdir myrepo; cd myrepo;
git clone git://repo.or.cz/fast-export.git .
rm -rf .git .gitignore
git init
./hg-fast-export.sh -r ../path/to/local/hg/repo
git clean -f # remove fast-export files

Windows 7 64-bit Solution

How to convert a Mercurial repo into a Git repo.

This is one of the most asinine sequences of events I have ever had to figure out, but I am not a Python guy at all.

My Start Environment

  • Visual Studio 2010/2012
  • TortoiseHg 2.8.2; Includes Mercurial 2.6.3 & Python 2.7.3
  • Git 1.8.3

Errors I Got Along the Way

  • Could not find Python
  • Could not find Mercurial module in Python
  • Easy_Install does not exists
  • Could not compile Mercurial module

What I Did

  • Had to install Python27 stand alone (http://www.python.org/)
  • Make sure to put path in Path in Environment Variables
  • Had to install Easy_Install (http://sourceforge.net/projects/pywin32/files/)
  • Make sure the Python Scripts path is also in Environment Variables
  • And get this... Had to go to MSDN and download full version of Visual Studio 2008 and only install C++ with the 64-bit option turned on (so Easy_Install could compile the Mercurial module for Python), but I could not get it to work with the Express edition because could not find the 64-bit option.

Steps

  • In Command Console (after everything is installed)
    • C:\easy_install mercurial

(If you installed everything you should see it download and compile the Mercurial module)

  • In Git Console
    • Follow steps by @mauvis-ledford and @benno (Thanks guys!!!)

Here is a simple example of how to export your local clone of a Mercurial repo to GitHub.

First, add the hggit extension to your ~/.hgrc file if you have not already done so:

[extensions]
hggit =

Then run the following commands from the root of your Mercurial repository:

hg bookmark -r default master
hg push -f git+ssh://[email protected]/bolinfest/plovr.git

Personally, I had to specify -f when running hg push, though I suspect it is because I had some complicated branches in my Mercurial repo. You may not find it necessary for your migration.

Make sure that you have the git+ssh: URI to your GitHub repo rather than the git: URI. (The git+ssh: URI is a bit harder to find in the new GitHub UI.)

Depending on the size of your repo, running hg push may take awhile, so be patient. When you are done, you should be able to see your project on GitHub. In this example, my project was available at https://github.com/bolinfest/plovr.

As you would expect, now anyone can clone your public GitHub repo as follows:

git clone [email protected]:bolinfest/plovr.git

As of today, git distribution includes a tool to convert mercurial repositories to git, hg-to-git. If you have a recent git version installed, it is there. Worked very well for me.


Here are all the pieces put together for a migration on Windows.

Prerequisites

  • Git
  • Mercurial or TortoiseHg
  • Python 2.7 (3.x won't work)

During install, allow binding to .sh files.
Ensure that all tools are available in the PATH environment variable.

Migration

  1. Open cmd.exe

  2. mkdir c:\git_work

  3. cd c:\git_work

  4. git clone http://repo.or.cz/r/fast-export.git
    This creates folder: c:\git_work\fast-export\

  5. Now you need mercurial libs for python. Either get them from here or do the following steps.
    Edit c:\git_work\fast-export\hg-fast-export.py:

    import sys # move this line up here
    # Add a new line that imports [mercurial libraries][2] from this zip:
    sys.path.append(r'C:\Program Files\TortoiseHg\lib\library.zip')
    # ...above the old line:
    from mercurial import node
    
  6. mkdir MyNewGitRepo

  7. Copy content of fast-export to MyNewGitRepo, ignore .git*

  8. hg-fast-import.sh -r c:\Path\To\MyOldHgRepo

  9. If this fails with "Error: repository has at least one unnamed head..." call the last line with parameter: --force

  10. Remove the migration scripts:

    git clean -n # see what would happen git clean -f # delete migration files

  11. Restore missing files by resetting the index and working tree.

    git reset --hard