Converting from Mercurial to Git

Recently, I had to convert one of my older projects from Mercurial to Git.

It turns out there’s an app for that. It’s called fast-export and it works very well, preserving all of your history and migrating from Mercurial to Git very quickly. The following steps walk you through the relatively simple process.

Note: If you’re a Mac OS X user and you used Macports to install Mercurial or Python, though, you’ll need to perform an extra step before undertaking the conversion. The following command, typed on the command line or pasted into your .profile or .bash_login file should do the trick:

export PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

Migrating

It’s a relatively simple process. First we download fast-export (the best way is via its Git repository, which I’ll clone right to the desktop), then we create a new git repository, perform the migration, and check out the HEAD. On the command line, it goes like this:

cd ~/Desktop
git clone git://repo.or.cz/fast-export.git
git init git_repo
cd git_repo
~/Desktop/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD

You should see a long listing of commits fly by as your project is migrated after running fast-export. If you see errors, they are likely related to an improperly specified Python path (see the note above and customize for your system).

That’s it, you’re done.

More articles in the Archive →