Our Blog

Engage with us

Now showing articles in "hg"

Migrating from Mercurial to Git

Posted 2013-06-12 15:31:43 GMT

My company recently migrated from Mercurial to Git for its DVCS. We had nothing against Mercurial, specifically - it is very well documented, easier to use than Git (IMHO), and has a better assortment of cross-platform tools available. As most of our web development becomes closely tied with Linux and OS X, however, the cross-platform advantages suddenly became less of a concern. And the tools and infrastructure around Git are vastly superior to that of Mercurial.

Starting with this guide, we were able to do the initial conversion to Git from Mercurial. That only got us part of the way there, however. It gave us a working master, but none of the other history we needed.

One of our developers noticed we only had the master branch, so back on the machine that did the initial conversion, we had to run

git push origin –all


And all the rest of our branches were back. All of them, including the ones we had closed. If you aren’t familiar with how Mercurial deals with branches, you can close branches that are no longer used and you don’t plan on working with again. Because Git deals with branches a bit differently, there is no similar construct available. Not wanting to see dozens of old, unused branches every time we ask Git for a list of them, we needed some sort of solution. Google to the rescue!

This post provided us with what we needed: an archival solution. With some hackery (thanks, Vim!) I was able to quickly hack a script together that pruned all the old branches. However, I forgot to prune them server side, too. Thanks again to Vim, I was able to quickly make another script to remotely delete branches:

git push origin –delete <branchName>


And at this point, I believed us to be done. Wrong! In looking through Atlassian Stash (we host our repos internally with Stash - an excellent app!), none of our archival tags were there. A little more research turned up this gem:

git push origin –tags


And finally, finally our git repository was on par with our Mercurial repository. Happy hacking to us!

tl;dr - Here’s a list of commands that we ran to do the migration. Insert the name of your repository in for repoName, and a branch you care about for branchName:

cd ~

git clone git://repo.or.cz/fast-export.git

mkdir repoName

cd repoName

git init

~/fast-export/hg-fast-export.sh -r /path/to/hg/repoName

# Get some coffee, it will be a bit

git checkout HEAD

git remote add origin http://server/repoName.git # Wherever your repo lives…

git push origin –all

# Run these next few for each branch you care about

git tag -m ‘Closing old branchName branch.’ archive/branchName branchName

git branch -D branchName

git push origin –delete branchName # For each branch you care about

# And finally…

git push origin –tags


This could be simplified some, but this should work well enough for you. Happy migrating!

Tags: dvcs git hg crome-plated