How do I convert a Bitbucket Mercurial repository (Hg repository) to a Git repository? I want to keep branches and commit history.
There's also the hg-git Mercurial plugin with which it's possible to convert HG repos to Git repos.
Here's the relevant part from the readme (section "Usage"):
Hg-Git can also be used to convert a Mercurial repository to Git. You can use
a local repository or a remote repository accessed via SSH, HTTP or HTTPS. Use
the following commands to convert the repository (it assumes you're running this
in $HOME).
$ mkdir git-repo; cd git-repo; git init; cd ..
$ cd hg-repo
$ hg bookmarks hg
$ hg push ../git-repo
The hg bookmark is necessary to prevent problems as otherwise hg-git
pushes to the currently checked out branch confusing Git. This will
create a branch named hg in the Git repository. To get the changes in
master use the following command (only necessary in the first run,
later just use git merge or rebase).
$ cd git-repo
$ git checkout -b master hg
Note that hg-git only understands Mercurial bookmarks, not Mercurial branches!
(because it was designed to convert into both directions, and there's no Git equivalent to Mercurial's named branches - Git branches are more like Mercurial bookmarks)
If your Mercurial repository has named branches that you want to keep, you need to create a bookmark for each of them before the conversion.
I tried GitHub's importer (mentioned in the other answer) as well, but liked hg-git better because I have more control over the end result.
What I didn't like about the GitHub importer is that it creates commits with "strange" email adresses.
Even though the commits in the HG repos have the same email adress as my GitHub user, the importer still asks me to map the commits to an existing GitHub user.
And even though I select my own user and the commits in the GitHub UI look like they were made by my user, the actual adresses in the repo (when I clone it) look something like this:
Christian Specht <123456+christianspecht@users.noreply.github.com>
With git-hg, you can create a text file with "mappings" from the existing HG committers to the desired Git committers.
Example from the link:
johnny = John Smith
dougie = Doug Johnson
So if the HG repository had commits from committer johnny
, they will automatically be changed to John Smith
in the converted Git repository.