For the longest time, on my personal and side projects, I leveraged Mercurial as a source control system. It worked great, and I was always happy with it; however, I recently came to the decision that I wanted to start using Git for my source control. I’ve been using Git a lot at my primary employment, and found it would be faster, easier, and overall better for my personal and side project source control. As a result, I decided to switch from Mercurial to Git, and have put together this little tutorial to on how-to convert a Mercurial repository to Git using hg-fast-export.
To convert my Mercurial repositories to Git, I decided to take advantage of a utility called hg-fast-export.
NOTE: This assumes you’ve already have Git installed on your environment. If you do not have Git installed, please visit https://git-scm.com/ to download and install the latest release of Git to your environment.
Step 1. Download hg-fast-export
To convert a Mercurial repository to Git, I recommend using a tool called hg-fast-export, which you’ll need to use git to clone.
git clone git://repo.or.cz/fast-export.git
Step 2. Create a New Git Repository
The next step is to create and initialize a new empty Git repository.
mkdir new_git_repository cd new_git_repository git init
Step 3. (Optional) Configure core.ignoreCase
If you find hg-fast-export complaining about the option core.ignoreCase being set to true in the Git repository, risking possible empty changesets for renamed files, you can set the option core.ignoreCase in the Git repository to false to ensure that renamed files add to the changesets.
git config core.ignoreCase false
Step 4. Execute hg-fast-export
The next step is to run the hg-fast-export utility from the newly created and initialized Git repository folder, specifying the path to the Mercurial repository you wish to export and import back into the Git repository.
/path/to/hg-fast-export.sh -r /path/to/hg_repo
Step 5. Checkout HEAD
The hg-fast-export utility does not automatically checkout the newly imported repository. You will need to follow up the import with a git checkout command to checkout the HEAD revision.
git checkout HEAD
Step 6. Push Git Repository Server-Side
To push the newly populated Git repository to a remote server, you’ll need to add a remote origin location, and execute a push to the remote origin/master branch.
git remote add origin git@my-git-server:my-repository.git git push -u origin master
Step 7. Copy over Untracked Files and Update .gitignore
Once this has been completed, you’ll also need to copy over any untracked files from the Mercurial repository to the Git repository, and modify the .gitignore file to specifically ignore the untracked files. You’ll then want to commit the .gitignore file to your local repository and push updates out to the remote server.