It’s simple to push Mercurial source code repositories to GitHub (or another git server). This is useful if you can’t, or don’t want to, switch off of Mercurial to git. You can take advantage of GitHub’s superior community and features (sorry, BitBucket) without having to puzzle over ____ (anti-git screed left as an exercise to the reader).
To do this, you’ll need the hg-git plugin for Mercurial, by Scott Chacon and others. The technique is outlined on hg-git’s read-me, but it still took me some effort to get working right, so I thought this blog post might be helpful.
I assume you already have Mercurial installed, a project in a Mercurial repository you want to push, and a GitHub account. Here’s how you do it.
Install hggit
If you’re using MacPorts and Python 2.6, you should be able to do this with:
sudo port install py26-hggit
At the end of hggit’s install process, hggit prints out a line to add to your ~/.hgrc
. For MacPorts & Python 2.6, this will look like:
hggit=/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/hggit
Note: Installing hggit in a virtualenv is probably not a good idea unless you also are using a version of Mercurial installed in the same virtualenv. You could probably make it work, but it’s likely more work than it’s worth.
Set up and connect to a GitHub repository
Create an empty repository on GitHub to store your Mercurial project. Copy the git URL from the new repository. Append git+ssh://
to the beginning, and change the :
to a /
. For example, this:
git@github.com:mercurialpoweruser/mercurialrocks.git
becomes this:
git+ssh://git@github.com/mercurialpoweruser/mercurialrocks.git
This URL then goes in the [paths]
section of your Mercurial repository’s .hg/hgrc
. If you want to push and pull from GitHub by default, it will look like this:
[paths] default = git+ssh://git@github.com/mercurialpoweruser/mercurialrocks.git
Or, if you already have a remote Mercurial repository that you want to keep using, you can add the GitHub repository with a different name:
[paths] default = ssh://hg@bitbucket.org/mercurialpoweruser/mercurialrocks github = git+ssh://git@github.com/mercurialpoweruser/mercurialrocks.git
Next, push your repository to GitHub for the first time, with hg push
(if GitHub is your default) or hg push github
(if GitHub isn’t the default).
Now, go to GitHub and reload the page, and revel in the feeling that you are living in a utopian future where cars fly, cold fusion supplies our electricity, and different DVCSs interoperate with each other.
Gotchas
If you find that Mercurial mysteriously stops pushing new changes to GitHub, even though there are new changes to push and it’s pushing them to Mercurial servers fine, you might want to take a look your bookmarks. Do this with hg bookmarks
. Make sure that the changeset ID for master is the changeset ID of tip: hg tip
. If they’re not the same, update the master bookmark with hg bookmark -f master
. This happened to me when I used an older version of Mercurial (1.3.1) without the bookmarks extension enabled. Don’t do that.
Your mileage may vary
I haven’t used this technique on repositories that have lots of branches, file renames, merge conflicts, or other “fancy” distributed version control features. I wouldn’t advise switching your entire development team and mission-critical repositories over to this technique without some more extensive testing.