Converting from Mercurial to Git

I’ve been a bit lazy, I have to admit. When Bitbucket shuttered its support for Mercurial and then eventually deleted all Mercurial repositories, I just carried on using Mercurial locally as I am a sole developer.

However, as I am now having to use git for client work, I have become comfortable with using git (especially via the excellent TortoiseGit) so it is time to convert.

In the spirit of DataHamster, this is a quick précis of the steps needed to do conversion, with particular reference to Bitbucket.

The procedure that I followed is from Helge Klein and the reason I used it over other methods is that it does not require you to download an additional plugin or extension. It just works. Well, it did for me anyway.

You can read the whole procedure at helgeklein.com/blog/2015/06/… but I will reproduce the basic steps here in case that link ever goes dead. No copyright infringement is intended by this! I give full credit to Helge Klein and do not claim the work as my own.

This procedure assumes that you are running Windows, and you have Git for Windows installed, and that you selected “Use Git from the Windows Command Prompt” when you installed it. It also assumes you are using Bitbucket as your host. However, it should still be useful even if you are not.

Preparation

First, make sure that the hggit extension is enabled.

If you are using TortoiseHg then you can do this by selecting Global Settings, clicking on Extensions, and then ticking “hggit”.

Alternatively, edit your mercurial config file (located at %userprofile%\mercurial.ini), and ensure that it has the following lines:

[extensions]
hggit = 
hgext.bookmarks =

[git]
intree = True

It’s worth doing that even if you are running TortoiseHg, in which case you would click the “Edit File” button after you have ticked the hggit extension.

Then, on a command line, verify that it is installed by running the following:

hg help hggit

That should yield some sensible-looking text.

If you get errors then it is not installed and you are probably going to need to google for some answers!

 

The conversion

Navigate to the folder that contains your Mercurial repo and open a command prompt.

Run the following command to make a bookmark of master for default, so a reference gets created:

hg bookmark -r default master

Run the following command to create the .git repository directory:

hg gexport --debug

You now have a bare Git repository, which we need to turn into a working repository.

Run

git config --bool core.bare false

then

git reset HEAD -- .

You should now have both a working hg and git repository.

Starting Over

If you made a mistake or forgot something and want to start over simply delete the .git subdirectory and run:

hg gclear

Uploading to Bitbucket

Ok, this is where I add something over and above what Helge wrote.

Go to Bitbucket and create a new git repository. But it needs to be totally empty! This is really important!

This is my whole reason for making this post, because if you allow Bitbucket to create any files then it isn’t going to work when you do your first push.

So, create the repo, making sure that you set “Include a README?” and “Include .gitignore?” to ‘No’.

Create the repository

And now you will see the commands you need to run to complete the procedure.

Now connect and push

So let’s run those.

If you then go to Bitbucket, and select Source, you will see that your repo has been imported. If you go to Commits, you will see your Mercurial commit history has been imported too. Sweet.

Finishing up

If your .hgignore file is in a git-compatible format (ie. is in glob syntax) then you may want to rename it to .gitignore, then commit and push.

git mv .hgignore .gitignore
git commit -m "Renamed" .\.gitignore .\.hgignore
git push

You can now go ahead and delete your .hg folder if you are no longer going to use Mercurial.

And that’s it! That was much more simple than I thought it would be!

I hope this is of use to someone. And, if it’s not, then that’s fine as it just reminds me what I need to do in future.

 

Tagged , , , , , . Bookmark the permalink.

About DataHamster

The Data Hamster stores facts and information in its capacious cheek pouches and regurgitates them from time to time.

Leave a Reply

Your email address will not be published.

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