Tech Chorus

Github For Mercurial User

written by Sudheer Satyanarayana on 2015-03-27

You are a Mercurial user and have little experience with Github. You are required to publish your Mercurial repository on Github. The good news is, you can do it without parting with Mercurial. There are bridges between Git and Mercurial. The one we are going to talk about today, is hg-git.

Let's start demonstrating our solution.

Step 1: Create a mercurial repository somewhere

I do it on Bitbucket. We'll call this our canonical repository. Clone the repository.

hg clone ssh://hg@bitbucket.org/bngsudheer/mycoolrepo

Step 2:Add some contents to the cloned repository

cd mycoolrepo/
echo "Sudheer Satyanarayana" >> contributors.txt
hg add contributors.txt
hg commit -m "Initial commit with contributors"

Step 3: Push the changes to the canonical Mercurial repository

hg push ssh://hg@bitbucket.org/bngsudheer/mycoolrepo

Step 4: Create the repository on Github

There are several ways to setup the mirror. One way to do it is importing the Mercurial repository to Gitbut. But in this article, we'll directly pull and push to Github repository. While creating the Git repository on Github, I chose the option - "Initialize this repository with a README".

Step 5: Enable the hg-git extension

Edit .hg/hgrc file in your repository and add these:

[extensions]
hggit =

Step 6: Pull from the newly created Git repository

hg pull git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Pulling for the first time from the newly created Git repository will create another head in your Mercurial repository. For example, running 'hg heads' shows:

changeset:   1:5b467a77b006
bookmark:    master
tag:         tip
parent:      -1:000000000000
user:        Sudheer Satyanarayana <sudheer.zzz@sudheer.net>
date:        Fri Mar 27 22:14:32 2015 +0530
summary:     Initial commit

changeset:   0:688bd09fe790
user:        sudheer
date:        Fri Mar 27 22:07:26 2015 +0530
summary:     Initial commit with contributors
The changeset 1:5b467a77b006 is bookmarked as 'master'.

Step 7: Merge the heads

hg merge master
hg commit -m "merging master"

Step 8: Push the changeset to the canonical Mercurial repository

hg push

Move the bookmark 'master' to default and push to Githib.

hg bookmark -r default master
hg push git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Now, you can adopt this method to work on the repository and keep pushing to both the repositories - The canonical Mercurial repository and the Github Git repository.

Work on the repository. Make some commits. Push to the canonical repository Move the bookmark 'master' to default and push to Githib Git repository If you do not move the bookmark, pushing to the Git repository makes no sense because hg-git will attempt to push the changeset the bookmark points to. In other words, the bookmark 'master' needs to be updated every time you want to push to the Git repository. You can automate the process of moving the bookmark to your latest line of work by activating the bookmark.

hg up master

That's it. Now your working method is:

Work on the repository. Make some commits. Push to the canonical repository Push to Githib Git repository Once the bookmark is active, Mercurial moves the bookmark to your latest line of work automatically.

Short repository name: For your convenience, you can give a short name to your Github repository URL. In your .hg/hgrc update the paths secion:

[paths]
default = ssh://hg@bitbucket.org/bngsudheer/mycoolrepo
github = git+ssh://git@github.com:bngsudheer/mycoolrepo.git

Notes: Make sure not to add '.git' directory. Github will not accept the changeset if there is a '.git' directory.