Migrate your Mercurial repo to Git using Cloud 9

So I’ve taken over a client’s web application from another company, but the challenge here is all the source code is managed using Mercurial. I am not a big fan of Mercurial, maybe just because I don’t really understand it and I don’t want to bother learning yet another VCS as I am very happy with using Git and it seems to be the standard.

This is very old project, so there is quite a long trail of commits. So I don’t want to just ignore that and start a new Git repo. We want to keep the history and just continue as normal, at the comfort of my existing Git knowledge.

So I’ve done some research and found that it is very possible. Two options came up, hg2git and hg-fast-export. They’re both supposed to work on all platforms, but very often it is quite a mission to get it working on Windows.

I also recently started using Cloud 9 IDE for my PHP and Node.js development as it’s so easy to setup containers without messing with my own local environment and it runs on Linux. The connection speed to Git and NPM is super quick, since it sits in the cloud and I can easily trash my environment without affecting anything else.

So I’ve found great tutorial online (https://git-scm.com/book/en/v2/Git-and-Other-Systems-Migrating-to-Git) that use the hg-fast-export module and put together a simple step-by-step script to setup and run on a Cloud 9 container:

  1. Create a new blank workspace on Cloud 9.
Select a blank workspace

2. Go to the Terminal window as all the action will happen in command line.

3. First thing you need to do is clone the hg-fast-export module to your environment. It will clone the module into a new fast-export directory. Enter the command below into your terminal window:
git clone https://github.com/frej/fast-export.git

4. The next step we need to clone our Mercurial repo to our development environment to a hg-repo directory:
hg clone [repo url] hg-repo

5. The next step would to generate an authors mapping file from the Mercurial repo. It will generate a authors file in the root directory.
hg log | grep user: | sort | uniq | sed ‘s/user: *//’ > ../authors

6. You need to edit the authors file and add the following line to the end of the file as this was one problem I ran into:
<>=devnull <devnull@localhost>

7. Create a new directory for your Git repo:
mkdir repo-git

8. Change to the new the directory:
cd repo-git

9. Initialize your directory to use Git:
git init

10. The next step we execute the fast-export module against our local Mercurial directory and our authors mapping file:
../fast-export/hg-fast-export.sh -r ../hg-repo -A ../authors

11. Create a new Git repository and add it as the origin:
git remote add origin [new Git URL]

12. Push your new Git repository to the remote Git server:
git push origin — all

It will keep all your branches, map your default branch to your master branch and all is well.

I hope this helps you and if you need any help give me shout. You can get my contact details on my website:
http://www.rudolphk.co.za/

--

--