[2014-12-26] Yeah Yeah, I Git It

I have migrated all my source-code repositories from Mercurial to Git. This means that they can now be mirrored on both Bitbucket as well as GitHub. It was not a very happy choice, but some times you just have to set aside your preferences and move on in life.

Beyond being very happy that we now have free and good-quality DVCS tools like Mercurial and Git (as well as free source-code hosting-providers for these like Bitbucket and GitHub respectively), I have never spent too much time and effort on VCS tools, considering them as tools (albeit important tools) to get a job done. To the extent that I did, I preferred the simplicity of Mercurial to the apparent complexity of Git (actually the command-line interfaces of the hg and git programs, respectively). I could easily memorize (or guess) the commands needed for Mercurial for day-to-day use, while Git seemed to constantly require looking up the relevant commands on-line and a hope that I have not screwed things up. It was also nice to see Mercurial (and Bitbucket) users quietly using their tool to actually develop software and quite off-putting to see Git (and GitHub) users constantly proselytize to (and putting down) users of other VCS tools. That is why I chose to use Mercurial to manage all my source-code repositories.

Unfortunately however, it is quite difficult to avoid Git (and GitHub) these days - more and more developers start their projects in Git (and on GitHub) and even those using Mercurial (and Bitbucket) earlier seem to have started converting their repositories to Git (for example, the Go project and even Python). Since I can't avoid learning Git (and using GitHub) and since it isn't actually that bad compared to Mercurial (in fact, the underlying concepts in Git are remarkably simple and consistent, while the git tool is incredibly fast; it also makes working with branches unbelievably simple and fast), I decided to just stick to Git. It also allows me to use both Bitbucket and GitHub for hosting my source-code repositories.

Fortunately the hg-git Mercurial plug-in makes this transition quite painless. You first install Dulwich and hg-git. Then you execute something like the following for each of your Mercurial repositories:

# Create an empty Git repository.
$ mkdir -p /path/to/git_repo
$ cd /path/to/git_repo
$ git init
 
# Export the Mercurial repository to the Git repository.
$ cd /path/to/hg_repo
$ hg bookmark hg
$ hg push /path/to/git_repo
 
# Finish the migration in the Git repository.
$ cd /path/to/git_repo
$ git checkout -b master hg
$ git branch -d hg
$ git tag -a git-conv -m "Converted to Git."

Before pushing this to Bitbucket, rename your existing Mercurial repository (say “MyProject” to “MyProject_Hg”) to create a back-up and create a Git repository with the original name (i.e. “MyProject”). On GitHub create a Git repository with the same name. Now you execute something like the following:

# Add the Bitbucket and GitHub Git remotes.
$ git remote add origin ssh://git@bitbucket.org/username/myproject.git
$ git remote set-url origin --add ssh://git@github.com/username/MyProject.git
 
# Push the Git repository to Bitbucket and GitHub.
$ git push -u origin master
$ git push origin git-conv

That was it - that was all I needed to migrate my Mercurial repositories to Git and to host them on Bitbucket and GitHub. See the hg-git and git documentation for the details. The freely-available book “Pro Git” provides an excellent introduction to Git (as well as its internals).

Farewell Mercurial, I will now be using Git. You were a great tool to work with, but the world is just not a fair place.

Other Posts from 2014