将 Mercurial 项目转换为 Git
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16037787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Mercurial project to Git
提问by gandolf
I need to convert a mercurial project to a git project, but I would like to keep the commit history intact. My current solution was to just remove hg related files and then git init && add manually the files I needed, but that would not keep the history. Are there any solutions to this?
我需要将 mercurial 项目转换为 git 项目,但我想保持提交历史完整。我目前的解决方案是删除 hg 相关文件,然后 git init && 手动添加我需要的文件,但这不会保留历史记录。有什么解决办法吗?
采纳答案by alexclooze
You can try using fast-export:
您可以尝试使用fast-export:
cd ~
git clone https://github.com/frej/fast-export.git
git init git_repo
cd git_repo
~/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD
Also have a look at this SO question.
也看看这个 SO question。
If you're using Mercurial version below 4.6, adrihanugot your back:
如果您使用的是低于 4.6 的 Mercurial 版本,adrihanu 会支持您:
As he stated in his comment: "In case you use Mercurial < 4.6 and you got "revsymbol not found" error. You need to update your Mercurial or downgrade fast-export by running git checkout tags/v180317 inside ~/fast-export directory.".
正如他在评论中所说:“如果您使用 Mercurial < 4.6 并且出现“找不到 revsymbol”错误。您需要通过在 ~/fast-export 目录中运行 git checkout tags/v180317 来更新您的 Mercurial 或降级快速导出.” .
回答by Timmmm
Ok I finally worked this out. This is using TortoiseHg on Windows. If you're not using that you can do it on the command line.
好的,我终于解决了这个问题。这是在 Windows 上使用 TortoiseHg。如果您不使用它,则可以在命令行上进行。
- Install TortoiseHg
- Right click an empty space in explorer, and go to the TortoiseHg settings:
- 安装 TortoiseHg
- 右键单击资源管理器中的空白区域,然后转到 TortoiseHg 设置:
- Enable
hggit
:
- 启用
hggit
:
Open a command line, enter an emptydirectory.
git init --bare .git
(If you don't use a bare repo you'll get an error likeabort: git remote error: refs/heads/master failed to update
cd
to your Mercurial repository.hg bookmarks hg
hg push c:/path/to/your/git/repo
In the Git directory:
git config --bool core.bare false
(Don't ask me why. Something about "work trees". Git is seriously unfriendly. I swear writing the actual code is easier than using Git.)
打开命令行,输入一个空目录。
git init --bare .git
(如果你不使用裸仓库,你会得到类似的错误abort: git remote error: refs/heads/master failed to update
cd
到您的 Mercurial 存储库。hg bookmarks hg
hg push c:/path/to/your/git/repo
在 Git 目录中:(
git config --bool core.bare false
不要问我为什么。关于“工作树”的东西。Git 非常不友好。我发誓编写实际代码比使用 Git 更容易。)
Hopefully it will work and then you can push from that new git repo to a non-bare one.
希望它会起作用,然后你可以从那个新的 git repo 推送到一个非裸的。
回答by atulkhatri
If you want to import your existing mercurial repository into a 'GitHub' repository, you can now simply use GitHub Importeravailable here[Login required]. No more messing around with fast-exportetc. (although its a very good tool)
如果要将现有的 mercurial 存储库导入“GitHub”存储库,现在只需使用此处提供的GitHub 导入器[需要登录]。不再纠结于快速导出等(虽然它是一个非常好的工具)
You will get all your commits, branchesand tagsintact. One more cool thing is that you can change the author's email-idas well. Check out below screenshots:
您将获得完整的所有提交、分支和标签。更酷的一件事是您还可以更改作者的电子邮件 ID。查看以下屏幕截图:
回答by Dion Truter
Some notes of my experience converting Mercurial to Git.
我将 Mercurial 转换为 Git 的一些经验笔记。
1. hg-fast-export
1. 汞快速出口
Using hg-fast-export failed and I needed --force as noted above. Next I got this error:
使用 hg-fast-export 失败,我需要 --force 如上所述。接下来我得到了这个错误:
error: cannot lock ref 'refs/heads/stable': 'refs/heads/stable/sub-branch-name' exists; cannot create 'refs/heads/stable'
错误:无法锁定 ref 'refs/heads/stable':'refs/heads/stable/sub-branch-name' 存在;无法创建 'refs/heads/stable'
Upon completion of the hg-fast-export I ended up with an amputated repo. I think that this repo had a good few orphaned branches and that hg-fast-export needs a somewhat idealised repo. This all seemed a bit rough around the edges, so I moved on to Kiln Harmony (http://blog.fogcreek.com/announcing-kiln-harmony-the-future-of-dvcs/)
完成 hg-fast-export 后,我得到了一个截断的 repo。我认为这个 repo 有很多孤立的分支,hg-fast-export 需要一个有点理想化的 repo。这一切似乎有点粗糙,所以我转向了 Kiln Harmony ( http://blog.fogcreek.com/announcing-kiln-harmony-the-future-of-dvcs/)
2. Kiln
2.窑
Kiln Harmony does not appear to exist on a free tier account as suggested above. I could choose between Git-only and Mercurial-only repos and there is no option to switch. I raised a support ticket and will share the result if they reply.
Kiln Harmony 似乎不存在于上述建议的免费等级帐户中。我可以在仅 Git 和仅 Mercurial 存储库之间进行选择,并且没有切换选项。我提出了支持票,如果他们回复,将分享结果。
3. hg-git
3. hg-git
The Hg-Git mercurial plugin (http://hg-git.github.io/) did work for me. FYI on Mac OSX I installed hg-git via macports as follows:
Hg-Git mercurial 插件 ( http://hg-git.github.io/) 对我有用。在 Mac OSX 上仅供参考,我通过 macports 安装了 hg-git,如下所示:
- sudo port install python27
- sudo port select --set python python27
- sudo port install py27-hggit
- vi ~/.hgrc
- 须藤端口安装python27
- 须藤端口选择 --set python python27
- 须藤端口安装 py27-hggit
- vi ~/.hgrc
.hgrc needs these lines:
.hgrc 需要这些行:
[ui]
username = Name Surname <[email protected]>
[extensions]
hgext.bookmarks =
hggit =
I then had success with:
然后我取得了成功:
hg push git+ssh://[email protected]:myaccount/myrepo.git
4. Caveat: Know your repo
4. 警告:了解你的回购
All the above are blunt instruments and I only pushed ahead because it took enough time to get the team to use git properly.
以上都是生硬的工具,我只是因为花了足够的时间让团队正确使用 git 才继续前进。
Upon first pushing the project per (3) I ended up with all new changes missing. This is because this line of code must be viewed as a guide only:
在第一次按 (3) 推动项目时,我最终丢失了所有新更改。这是因为这行代码只能作为指南:
$ hg bookmark -r default master # make a bookmark of master for default, so a ref gets created
The theory is that the default branch can be deemed to be master when pushing to git, and in my case I inherited a repo where they used 'stable' as the equivalent of master. Moreover, I also discovered that the tip of the repo was a hotfix not yet merged with the 'stable' branch.
理论是,在推送到 git 时,默认分支可以被视为 master,在我的情况下,我继承了一个 repo,其中他们使用 'stable' 作为 master 的等价物。此外,我还发现 repo 的提示是一个尚未与“稳定”分支合并的修补程序。
Without properly understanding both Mercurial and the repo to be converted, you are probably better off not doing the conversion.
如果没有正确理解 Mercurial 和要转换的存储库,您最好不要进行转换。
I did the following in order to get the repo ready for a second conversion attempt:
我做了以下操作以使 repo 准备好进行第二次转换尝试:
hg update -C stable
hg merge stable/hotfix-feature
hg ci -m "Merge with stable branch"
hg push git+ssh://[email protected]:myaccount/myrepo.git
After this I had a verifiably equivalent project in git, however all the orphaned branches I mentioned earlier are gone. I don't think that is too serious, but I may well live to regret this as an oversight. Therefore my final thought is to keep the original anyway.
在此之后,我在 git 中有一个可验证的等效项目,但是我之前提到的所有孤立分支都消失了。我不认为这太严重,但我很可能会因为疏忽而后悔。因此,我最后的想法是无论如何都要保留原件。
Edit: If you just want the latest commit in git, this is simpler than the above merge:
编辑:如果你只想要 git 中的最新提交,这比上面的合并更简单:
hg book -r tip master
hg push git+ssh://[email protected]:myaccount/myrepo.git
回答by Matthias
I had a similar task to do, but it contained some aspects that were not sufficiently covered by the other answers here:
我有一个类似的任务要做,但它包含一些其他答案没有充分涵盖的方面:
- I wanted to convert all(in my case: two, or in general: more than one) branches of my repo.
- I had non-ASCII and (being a Windows user) non-UTF8-encoded characters (for the curious: German umlaute) in my commit messages and file names.
- 我想转换我的回购的所有(在我的情况下:两个,或者一般来说:多个)分支。
- 我的提交消息和文件名中有非 ASCII 和(作为 Windows 用户)非 UTF8 编码的字符(为了好奇:德语变音)。
I did not try fast-export and hg-fast-export, since they require that you have Python and some Mercurial Python modules on your machine, which I didn't have.
我没有尝试 fast-export 和 hg-fast-export,因为它们要求你的机器上有 Python 和一些 Mercurial Python 模块,而我没有。
I did try hg-init with TortoiseHG, and this answergave me a good start. But it looked like it only converts the current branch, not all at once (*). So I read the hg-init docsand this blog postand added
我确实用 TortoiseHG 尝试过 hg-init,这个答案给了我一个好的开始。但看起来它只转换当前分支,而不是一次(*)。所以我阅读了hg-init 文档和这篇博客文章并添加了
[git]
branch_bookmark_suffix=_bookmark
to my mercurial.ini, and did
到我的 mercurial.ini,并做了
hg bookmarks -r default master
hg bookmarks -r my_branch my_branch_bookmark
hg gexport
(Repeat the 2nd line for every branch you want to convert, and repeat it again if you should happen to do another commit before executing the 3rd line). This creates a folder git
within .hg
, which turns out to be a bare Git repo with all the exported branches. I could clone this repo and had a working copy as desired.
(对要转换的每个分支重复第二行,如果在执行第三行之前碰巧再次提交,请再次重复)。这会git
在 中创建一个文件夹.hg
,结果是一个带有所有导出分支的裸 Git 存储库。我可以克隆这个 repo 并根据需要有一个工作副本。
Or almost...
或者几乎...
Running
跑步
git status
on my working copy showed all files with non-ASCII characters in their names as untracked files. So I continued researching and followed this advice:
在我的工作副本中,所有名称中包含非 ASCII 字符的文件都显示为未跟踪文件。所以我继续研究并遵循了这个建议:
git rm -rf --cached \*
git add --all
git commit
And finally the repo was ready to be pushed up to Bitbucket :-)
最后,repo 已准备好推送到 Bitbucket :-)
I also tried the Github importer as mentioned in this answer. I used Bitbucket as the source system, and Github did quite a good job, i.e. it converted all branches automatically. However, it showed '?'-characters for all non-ASCII characters in my commit messages (Web-UI and locally) and filenames (Web-UI only), and while I could fix the filenames as described above, I had no idea what to do with the commit messages, and so I'd prefer the hg-init approach. Without the encoding issue the Github importer would have been a perfect and fast solution (as long as you have a paid Github account or can tolerate that your repo is public for as long as it takes to pull it from Github to your local machine).
我还尝试了这个答案中提到的 Github 导入器。我使用Bitbucket作为源系统,Github做得很好,即自动转换所有分支。但是,它在我的提交消息(Web-UI 和本地)和文件名(仅限 Web-UI)中显示了所有非 ASCII 字符的“?”字符,虽然我可以如上所述修复文件名,但我不知道如何处理提交消息,所以我更喜欢 hg-init 方法。如果没有编码问题,Github 导入器将是一个完美而快速的解决方案(只要您有一个付费的 Github 帐户,或者可以容忍您的 repo 是公开的,只要它需要将它从 Github 拉到您的本地机器)。
(*) So it looked like beforeI discovered that I have to bookmark all the branches I want to export. If you do and push to a bare(!) repo, like the linked answer says, you get all the branches.
(*) 所以在我发现我必须为所有要导出的分支添加书签之前,它看起来很像。如果你这样做并推送到一个裸(!) 存储库,就像链接的答案所说的那样,你会得到所有的分支。
回答by Reck Hou
From:
从:
http://hivelogic.com/articles/converting-from-mercurial-to-git
http://hivelogic.com/articles/converting-from-mercurial-to-git
Migrating
It's a relatively simple process. First we download fast-export (the best way is via its Git repository, which I'll clone right to the desktop), then we create a new git repository, perform the migration, and check out the HEAD. On the command line, it goes like this:
迁移
这是一个相对简单的过程。首先我们下载 fast-export(最好的方法是通过它的 Git 存储库,我将直接克隆到桌面),然后我们创建一个新的 git 存储库,执行迁移,并检查 HEAD。在命令行上,它是这样的:
cd ~/Desktop
git clone git://repo.or.cz/fast-export.git
git init git_repo
cd git_repo
~/Desktop/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD
You should see a long listing of commits fly by as your project is migrated after running fast-export. If you see errors, they are likely related to an improperly specified Python path (see the note above and customize for your system).
That's it, you're done.
当您的项目在运行 fast-export 后迁移时,您应该会看到一长串提交列表。如果您看到错误,则它们很可能与未正确指定的 Python 路径有关(请参阅上面的注释并针对您的系统进行自定义)。
就是这样,你完成了。
回答by Ry4an Brase
Another option is to create a free Kiln account -- kiln round trips between git and hg with 100% metadata retention, so you can use it for a one time convert or use it to access a repository using whichever client you prefer.
另一种选择是创建一个免费的 Kiln 帐户——在 git 和 hg 之间进行 kiln 往返并具有 100% 元数据保留,因此您可以使用它进行一次转换或使用您喜欢的任何客户端访问存储库。
回答by Sashah
This would be better as a comment, sorry I do not have commenting permissions.
这作为评论会更好,抱歉我没有评论权限。
@mar10 comment was the missing piece I needed to do this.
@mar10 评论是我需要这样做的缺失部分。
Note that '/path/to/old/mercurial_repo' must be a path on the file system (not a URL), so you have to clone the original repository before. – mar10 Dec 27 '13 at 16:30
请注意,'/path/to/old/mercurial_repo' 必须是文件系统上的路径(不是 URL),因此您必须先克隆原始存储库。– mar10 13 年 12 月 27 日 16:30
This comment was in regards to the answer that solved this for me, https://stackoverflow.com/a/10710294/2148757which is the same answer as the one marked correct here, https://stackoverflow.com/a/16037861/2148757
此评论与为我解决此问题的答案有关,https://stackoverflow.com/a/10710294/2148757与此处标记为正确的答案相同,https://stackoverflow.com/a/16037861 /2148757
This moved our hg project to git with the commit history intact.
这将我们的 hg 项目移至 git 并保持完整的提交历史。