将Bitbucket Mercurial存储库转换为Git。维护分支机构和历史记录。在线解决方案

use*_*003 7 git mercurial bitbucket

如何将Bitbucket Mercurial存储库(Hg存储库)转换为Git存储库?我想保留分支并提交历史记录。

use*_*003 9

我发现将Mercurial存储库转换为Git并保留所有分支的唯一方法是使用GitHub的importer。这也是最简单的方法,因为它们都是在线完成的。无需本地安装,无需命令行。

大意

使用Github的进口商将Bitbucket Mercurial存储库转换为GitHub Git存储库。然后将其导入回Bitbucket。

步骤

在Bitbucket中:

重命名要转换的Mercurial存储库(如果您希望新的Git存储库具有与Mercurial存储库相同的名称)。您可以在“设置”中执行此操作。例如,将其命名为projectname_mercurial。

抓取存储库的克隆URL。(在存储库的主页上,单击“克隆”并仅获取URL,例如https://username@bitbucket.org/username/projectname

在Github中:

如果您还没有帐户,请创建一个https://github.com

使用GitHub导入器https://github.com/new/import

  • 输入来自Bitbucket的克隆URL。
  • 给它起个名字。到底什么都没关系。
  • 将其设置为私有。默认为公开!

    开始导入

等待!提示您可以的页面不要关闭(我们会通过电子邮件发送给您...)。等待它询问您回购凭证。这可能需要几分钟。然后输入凭据。(假设它需要凭据。)

再次等待!再次会要求您提供回购凭证。至少每次都对我有用。

现在,您等待完成。您会收到一封电子邮件。

完成后,获取GitHub项目URL。当您位于GitHub项目页面上时,这只是网页URL。

在Bitbucket中:

使用“ +”>“导入”>“存储库”菜单选项创建一个新的存储库。

  • 输入GitHub项目URL
  • 选中“需要身份验证”,输入您的GitHub凭据。
  • 输入新的Git存储库名称(例如,重命名之前的Mercurial项目的名称。)
  • 将访问级别保持为私有。

在Github中:

删除导入的项目:“项目”的“设置”选项卡>“删除此存储库”

  • 似乎工作得很好,尽管我决定在 GitHub 中的输入成功后停止,而只是删除 Bitbucket 存储库。;-) (5认同)
  • 感谢您提供本手册。请注意,GitHub 错误地导入包含非 ASCII 符号的提交消息。显然存在编码问题。因此,过去在 Bitbucket Mercurial 存储库中正确显示的文本将在新的 Bitbucket Git 存储库中包含问号。 (3认同)
  • 您无需等待凭据提示,只需将它们直接包含到导入 URL 中即可:https://username:password@bitbucket.org/username/projectname (2认同)

Chr*_*cht 7

还有hg-git Mercurial 插件可以将 HG 存储库转换为 Git 存储库。

这是自述文件中的相关部分(“用法”部分):

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
Run Code Online (Sandbox Code Playgroud)

请注意, hg-git 只理解 Mercurial书签,而不理解Mercurial分支
(因为它被设计成双向转换,并且没有与 Mercurial 的命名分支等效的 Git - Git 分支更像是 Mercurial 书签)

如果您的 Mercurial 存储库具有要保留的命名分支,则需要在转换前为每个分支创建书签。


我也尝试了 GitHub 的导入器(在另一个答案中提到过),但更喜欢 hg-git,因为我对最终结果有更多的控制权。

我不喜欢 GitHub 导入器的一点是它使用“奇怪”的电子邮件地址创建提交。
即使 HG 存储库中的提交与我的 GitHub 用户具有相同的电子邮件地址,导入程序仍然要求我将提交映射到现有的 GitHub 用户。
即使我选择了我自己的用户并且 GitHub UI 中的提交看起来像是由我的用户制作的,但 repo 中的实际地址(当我克隆它时)看起来像这样:

Christian Specht <123456+christianspecht@users.noreply.github.com>
Run Code Online (Sandbox Code Playgroud)

使用 git-hg,您可以创建一个文本文件,其中包含从现有 HG 提交者到所需 Git 提交者的“映射”
来自链接的示例:

johnny = John Smith <jsmith@foo.com>
dougie = Doug Johnson <dougiej@bar.com>
Run Code Online (Sandbox Code Playgroud)

因此,如果 HG 存储库有来自 committer 的提交johnny,它们将自动更改为John Smith <jsmith@foo.com>转换后的 Git 存储库。