Convert your Mercurial Repository to Git on Windows

Back in the day when I started Uni, I had no idea what Version Control was. So, when tasked to choose my repository I just picked up what a friend of mine recommended. I then installed TortoiseHG on my Windows machine, created a BitBucket account and there I was, using Mercurial as my repository of choice.

I then realised that many many many many people use Github to share code and work on open source projects. Also, I discovered Github does not use Mercurial but uses Git as their distributed version control system.

For this reason I decided to try Git and learn how it works. My very first question was: how can I move Mercurial repositories into Git? I could just copy/paste the projects in the state they are and create a brand new Git repository with them. But what if… what if I wanted to move them to Git and keep all the previous changelists. How do I do it? Let me show you.

Prerequisite – Install Git and TortoiseHG (Windows)

First, I had to install Git (of course!) You can follow this link that has a super useful guide to do so and it’ll tell you all you need to know. Next install TortoiseHG from here: I use it as a Mercurial client but it also has a neat and useful extension which we are going to use to convert Hg to Git, called hggit.

Once everything is installed there are a few extra steps before the convertion.

Enable hggit in TortoiseHG

Open TortoiseHG workbench, go to File -> Settings and the Extension tab: select hggit.

Now that is enabled, we need to add a few extra settings to our mercurial settings file. This file is usually found in C:\Users\YOUR_USER\mercurial.ini

Add the following to the file:

hgext.bookmarks =

[git]
intree = True

Convert Mercurial to Git

In order to do the conversion you’ll need to open your Powershell / Command Prompt, navigate to your mercurial repository and then execute in order:

hg bookmark -r default master
hg gexport --debug

This creates a git bare repository. Bare repositories are generally used for sharing purposes, so what you need is a working directory instead: that is where you will be able to do ‘commit’ and ‘push’ onto your repository. To do this, you have to set the bare flag of your repository to false and reset the repository to start fresh (still in Powershell or Command Prompt):

git config --bool core.bare false
git reset HEAD -- .

Almost there! your folder structure should look like this

You can delete the .hg folder and there you have a git repository.

You can check the status of your repository to make sure everything is in place:

PS D:\Dev\UnityChat> git status
On branch master
nothing to commit, working tree clean

If anything has gone wrong you can reset the repository with:

 hg gclear 

command.

Push your Git repository to Github

Go to your github account and create a new repository. You can do this by clicking the ‘+’ sign on your profile page.

Once the repository is created you can push your local repository on Github with:

PS D:\Dev\UnityChat> git remote add origin https://github.com/lormori/UnityChat.git
PS D:\Dev\UnityChat> git push -u origin master
Enumerating objects: 138, done.
Counting objects: 100% (138/138), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (123/123), done.
Writing objects: 100% (138/138), 73.24 KiB | 1.46 MiB/s, done.
Total 138 (delta 80), reused 0 (delta 0)
remote: Resolving deltas: 100% (80/80), done.
To https://github.com/lormori/UnityChat.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

And there you have it, your whole project is now on Github with your previous ChangeLists. You can carry on working on it and ‘commit’ new stuff this way:

PS D:\Dev\UnityChat> git commit -a -m "test commit"
[master f78ecd0] test commit
1 file changed, 1 insertion(+), 1 deletion(-)
PS D:\Dev\UnityChat> git push
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 483 bytes | 241.00 KiB/s, done.
Total 6 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
To https://github.com/lormori/UnityChat.git
3bdd378..f78ecd0 master -> master

Conclusion

It is relatively easy to convert a Mercurial repository to Git, if you wish to do so. Just install Git and TortoiseHG on your machine and you are closer than you think you are! Once it is all up and running, you can now use your Git repository with the command line, but there are a few visual clients you can use instead. You can just use TortoiseHG with the hggit extension enabled, or you could download SourceTree, which seems very good for the job. In the end, it is up to you which tools you prefer to use while working: what matters is to get the job done.

I hope you’ll find this useful, and just in case you want to have a look, you can check out my new shiny github repository that I created this way. It still contains all the old commits from the mercurial one!

References and further reading

https://helgeklein.com/blog/2015/06/converting-mercurial-repositories-to-git-on-windows/ – Major article I followed
https://www.sourcetreeapp.com/ – Cool visual client which I might adopt
http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/
https://gist.github.com/seankross/8830032
https://stackoverflow.com/questions/11656761/git-please-tell-me-who-you-are-error
https://gitforwindows.org/
https://help.github.com/articles/create-a-repo/
http://www.rockstarprogrammer.org/post/2008/apr/06/differences-between-mercurial-and-git/
https://importantshock.wordpress.com/2008/08/07/git-vs-mercurial/
https://ericsink.com/entries/hg_denzel.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.