如何将Bitbucket Mercurial存储库(Hg存储库)转换为Git存储库?我想保留分支并提交历史记录。
答案 0 :(得分:11)
我发现在保留所有分支的同时将Mercurial存储库转换为Git的唯一方法是使用GitHub的导入器。这也是最简单的方法,因为它们都是在线完成的。无需本地安装,无需命令行。
主要思想
使用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
将其设置为私有。默认为公开!
开始导入
等待!提示您可以的页面不要关闭(我们会通过电子邮件发送给您...)。等待它询问您回购凭证。这可能需要几分钟。然后输入凭据。 (假设它需要凭据。)
再次等待!再次会要求您提供回购凭证。至少每次都对我有用。
现在,您要等到完成为止。您会收到一封电子邮件。
完成后,获取GitHub项目URL。当您位于GitHub项目页面上时,这只是网页URL。
在Bitbucket中:
使用“ +”>“导入”>“存储库”菜单选项创建一个新的存储库。
在Github中:
删除导入的项目: 项目的“设置”标签>删除此存储库
答案 1 :(得分:7)
还有hg-git Mercurial plugin,可以将HG仓库转换为Git仓库。
以下是自述文件(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
请注意,hg-git只理解Mercurial 书签,而不理解Mercurial 分支!
(因为它被设计为可以双向转换,而且没有Git等同于Mercurial的命名分支-Git分支更像是Mercurial书签)
如果您的Mercurial存储库中有您要保留的命名分支,则需要在转换之前为每个分支创建一个书签。
我也尝试了GitHub的importer(在另一个答案中提到),但是更好地喜欢hg-git,因为我对最终结果有更多的控制权。
我对GitHub导入器不满意的是,它使用“奇怪的”电子邮件地址创建提交。
即使HG仓库中的提交与我的GitHub用户具有相同的电子邮件地址,但导入程序仍要求我将提交映射到现有GitHub用户。
即使我选择了自己的用户,并且GitHub UI中的提交看起来都像是我的用户所做的,但回购中的实际地址(当我克隆它时)看起来像这样:
Christian Specht <123456+christianspecht@users.noreply.github.com>
使用git-hg,您可以create a text file with "mappings" from the existing HG committers to the desired Git committers。
链接中的示例:
johnny = John Smith <jsmith@foo.com>
dougie = Doug Johnson <dougiej@bar.com>
因此,如果HG存储库中有来自提交者johnny
的提交,则它们将在转换后的Git存储库中自动更改为John Smith <jsmith@foo.com>
。
答案 2 :(得分:1)
我发现https://github.com/frej/fast-export可以将Mercurial分支正确迁移到Git分支(与hg-git相比,后者只能与Mercurial 书签一起使用)。
快速导出可在本地(副本)存储库上工作,因此它并不纯粹是在线的。快速导出说明了如何创建新的本地Git存储库并填充它。
在我的情况下,我在Mercurial中留下了多个分支头,给出了此错误:
Error: repository has at least one unnamed head: hg r493
我可以使用“ hg strip 493”或合并以摆脱这些头。
我还必须使用“作者文件”和-A开关快速导出以映射格式不正确的作者字段来修复某些提交的作者字段,如自述文件中所述。
此后,快速导出接受了该回购。
您还需要通过Bitbucket Web界面创建一个新的Bitbucket存储库。
然后推送到Bitbucket。此示例使用已配置的SSH进行设置:
git remote add origin git@bitbucket.org:<user_name>/<repo_name>
git push --all origin
答案 3 :(得分:0)