Migrate Mercurial hg repository to git keeping the whole history

5 12 2015

Although hg and git share the same idea of versioning files, git provides much more tools to help teams to maintain code, releases and fixes. That’s why I decided to migrate project repository from hg to git. The method I describe preserves the whole hg history, branches and tags.

Migration with fast-export tool

Fast-export is tool that processes each hg commit and migrates it to git. Tool has the python version that you can run on windows, but it didn’t work for me so i used .sh script on linux on VirtualBox. Steps to migrate are:

  1. clone the fast-export git repo
  2. init your target git repo in path of your choice
  3. run the hg-fast-export.sh script and wait until it finished (migrating 4k commits takes around 20 minutes)
  4. start using your new git repo (checkout HEAD, add remote, push)

In other words execute those commands:

git clone git://repo.or.cz/fast-export.git
git init your_git_repo
cd your_git_repo
~/fast-export/hg-fast-export.sh -r /path_to_old_mercurial_repo
git checkout HEAD

Which will be master branch?

As a default behaviour your default mercurial branch will become master in git.

Filenames containing special characters

If you have filenames with special regional characters (like ą, ó, €) make sure to rename them in your hg repo before migration since, depending on your OS, they may migrate to even more strange chars and you may have problems deleting them from your new git repo.

Nested .git repos in your mercurial codebase

If you have commited the whole git repo and versioned it on mercurial you will not be able to push to the remote git repo after migration. It will fail with error that your new git repo already contains nested .git repos:

remote: error: object a9ee490ac00987835de30bdbc851da5e8d45d28b:contains '.git'

You need to clean up any .git folders from your hg before running migration scripts. Fortunately there is a tool to do it: BFG repo cleaner. Download the bfg.jar and issue the command to delete all .git files from hg repo:

java -jar bfg.jar --delete-folders .git --delete-files .git --no-blob-protection my-repo.git

After the migration and solving all problems described above my new git repo works like a charm!



Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: