It was time -- cringe -- to figure out how to export my mercurial repositories to git.
(TL;DR — fast-export worked for me, but with a catch. See below.)
I’ve been observing for some time how bitbucket.org cannot quite measure up to the potential of github.com when it comes to promoting the building of Open Source Software. It seems I've just been waiting for that proverbial kick in the pants to get off my adverbial butt and lead my ragtag bunch of repos out of the feature-desert of bitbucket and into the Open Source promised land of github.
(Now, I too shall have all manner of pieces of CI/CD flair on my READMEs!)
My procrastination all centered around transitioning from mercurial (hg) to git. I have no shortage of experience in git, here in 2019 where we are long past the ambiguity of whether mercurial or git would become the source control worship of choice.
However, dark ominous clouds swirled around shuttling my repository histories from one system to the next.
For when I went looking for utilities to help with this process, I found but two:
Very quickly did I audition both, and out of the box both were failures.
Argh! Why, O great and powerful Open Source, do you torment me so?
As ultimately I did not get hg-git working, this article will simply detail the steps I took to get fast-export to perform the correct rituals, along with the main problem I encountered and how I solved it.
or, what we’re trying to accomplish:
- Create a new empty git repo to serve as the target container for your export.
- Locally, set up your mercurial repo as a git repo. Set settings to make sure git ignores hg effluvia.
- Configure the repo to take the newly-created git repo from step 1 as its remote.
- Perform the export of HG history using fast-export.
- git commit && git push —set-upstream origin master
- Check new repo on github to make sure everything’s there.
or, what we actually have to do.
First off, you should go create a new empty git repo on whatever service you're using. Make sure you keep handy the remote information for use later on in the process.
Now I'm going to take the unusual step of starting with the problem that occurs in the middle, so that the instructions as a whole make more sense.
When running the fast-export.sh script, many people encounter the following unhelpful error message:
Traceback (most recent call last): File "/home/nthmost/fast-export-190107/hg-fast-export.py", line 7, in <module> from mercurial.scmutil import revsymbol ImportError: cannot import name revsymbol
This means it definitely didn’t do the export. What should we do?
Well, it turns out that several releases of the fast-export tool over the past year or more have been unreliable. I found, by poking around the Issues, that the only reliable recent-enough release is tagged “v180317”.
So what you should do is the following:
git clone https://github.com/frej/fast-export.git
git checkout tags/v180317
Once you have this specific version of fast-export, now you can continue on with the next steps:
../fast-export/hg-fast-export.sh -r /your/hg/repo
git remote add origin https://github.com/user/repo.git
git push —set-upstream origin master
Note that it’s entirely kosher simply to do all the git work directly within the mercurial directory you started with. If so, you can just replace
/your/hg/repo with a
. for the
If you want to keep on using mercurial with git, then you should probably get hg-git working; otherwise, I’d recommend only continuing to develop this project after you’ve cloned from the git repo afresh and wiped out all traces of your former mercurial life. 🙂
That’s all! I hope that’s perhaps saved you a few minutes of banging your head on a keyboard. Keep the faith!