Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bitbucket Mercurial repository to Git. Maintain branches and history. Online solution

How do I convert a Bitbucket Mercurial repository (Hg repository) to a Git repository? I want to keep branches and commit history.

like image 948
user984003 Avatar asked Aug 29 '19 19:08

user984003


People also ask

Is Mercurial better than Git?

Git Is Better for Experienced Users Mercurial depends on your level of technical expertise. Mercurial may be safer for less experienced users, but Git can offer ways to enhance safety (once you know what you are doing).

Does bitbucket use Git or Mercurial?

Bitbucket, owned by Atlassian, is a web-based version control repository hosting service, for source code and development projects. It has used Mercurial since the beginning in 2008 and then Git since October 2011.

Does bitbucket use Mercurial?

Bad news for Mercurial users: Atlassian has removed support for the Mercurial version control system from Bitbucket Cloud and its API. Mercurial repositories and features were officially removed from Bitbucket and the API on July 1, 2020.

Can you use Mercurial with GitHub?

The answer is: yes, it can! You will be able to do that by using the Mercurial bookmark, which are the Mercurial counterpart of Git branches and are used as such by the hggit plugin.


2 Answers

The only way I found to convert a Mercurial repo to Git, while keeping all the branches, was to use GitHub's importer. This was also the easiest since it's all done online. Nothing local to install, no command line stuff.

Main idea

Use Github's importer to convert Bitbucket Mercurial repo to GitHub Git repo. Then import that back into Bitbucket.

The steps

In Bitbucket:

Rename the Mercurial repository that you want to convert (if you want the new Git repo to have the same name as your Mercurial repo did). You do this in Repository Settings. For example, name it projectname_mercurial.

Grab the Clone URL of the repository. (On the repo's main page, click Clone and grab just the URL, like https://[email protected]/username/projectname )

In Github:

If you don't already have an account, create one https://github.com

Use the GitHub importer https://github.com/new/import

  • Enter the Clone URL from Bitbucket.
  • Give it a name. It doesn't really matter what.
  • Set it to private. Public is default!

NOTE: You can add your password to the URL directly, which will then avoid the need to enter your credentials in the steps below (thanks to @keith-turkowski comment below). So change your bitbucket clone url by adding :password after username, like this: https://username:[email protected]/username/projectname

Begin Import in Github

WAIT! Do not close the page when it says that you can (We'll email you...). Wait for it to ask you for the repo credentials. This can take several minutes. Then enter the credentials. (Assuming it requires credentials.)

WAIT AGAIN! It's again going to ask for your repo credentials. At least it did for me, every time. (See note above on adding your password directly to URL so it does not ask you.)

Now you wait until it's done. You will get an email.

When it's done, grab the GitHub project URL. It's just the webpage URL when you are on the GitHub project page.

In Bitbucket:

Create a new repository using the "+" > Import > Repository menu option.

  • Enter the GitHub project URL
  • Check "Requires authentication" Enter your GitHub credentials.
  • Enter the new Git repository name (For example, the name of your Mercurial project before you renamed it.)
  • Keep Access level as Private.

In Github:

Delete the imported project: Project's Settings tab > Delete this repository

like image 126
user984003 Avatar answered Sep 18 '22 17:09

user984003


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 <[email protected]> 

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 <[email protected]> dougie = Doug Johnson <[email protected]> 

So if the HG repository had commits from committer johnny, they will automatically be changed to John Smith <[email protected]> in the converted Git repository.

like image 27
Christian Specht Avatar answered Sep 19 '22 17:09

Christian Specht