将 Mercurial 存储库转换为 Git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389307/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:49:42  来源:igfitidea点击:

Convert a Mercurial Repository to Git

gitmercurial

提问by Tigraine

I've already tried hg2git through fast-export and I've already tried hg-git.

我已经通过快速导出尝试过 hg2git 并且我已经尝试过 hg-git。

Both with no success. hg2git actually worked, but I had to ask a friend who runs a Unix machine to do it. And that messed up all the linefeeds throughout the files.

两者都没有成功。hg2git 确实有效,但我不得不请一位运行 Unix 机器的朋友来做。这弄乱了整个文件中的所有换行符。

hg-git simply failed with some libzip compression error.

hg-git 只是因为一些 libzip 压缩错误而失败。

Has anyone had any success converting a hg repo to git on Windows?

有没有人在 Windows 上成功地将 hg repo 转换为 git?

To be clear, I don't care about interop. I want to port the whole repo from hg to git, taking the history with me. I don't need to push changes back and forth - only convert once.

需要明确的是,我不关心互操作。我想将整个 repo 从 hg 移植到 git,并随身携带历史。我不需要来回推动更改 - 只需转换一次。

回答by Mauvis Ledford

Did the following on my Mac to successfully export a Mercurial repo to Git (with full branches):

在我的 Mac 上执行以下操作以成功将 Mercurial 存储库导出到 Git(具有完整分支):

mkdir myrepo; cd myrepo;
git clone git://repo.or.cz/fast-export.git .
rm -rf .git .gitignore
git init
./hg-fast-export.sh -r ../path/to/local/hg/repo
git clean -f # remove fast-export files

回答by Trey Gramann

Windows 7 64-bit Solution

Windows 7 64 位解决方案

How to convert a Mercurial repo into a Git repo.

如何将 Mercurial 存储库转换为 Git 存储库。

This is one of the most asinine sequences of events I have ever had to figure out, but I am not a Python guy at all.

这是我必须弄清楚的最愚蠢的事件序列之一,但我根本不是 Python 人。

My Start Environment

我的开始环境

  • Visual Studio 2010/2012
  • TortoiseHg 2.8.2; Includes Mercurial 2.6.3 & Python 2.7.3
  • Git 1.8.3
  • Visual Studio 2010/2012
  • TortoiseHg 2.8.2;包括 Mercurial 2.6.3 和 Python 2.7.3
  • git 1.8.3

Errors I Got Along the Way

我一路走来的错误

  • Could not find Python
  • Could not find Mercurial module in Python
  • Easy_Install does not exists
  • Could not compile Mercurial module
  • 找不到 Python
  • 在 Python 中找不到 Mercurial 模块
  • Easy_Install 不存在
  • 无法编译 Mercurial 模块

What I Did

我做了什么

  • Had to install Python27 stand alone (http://www.python.org/)
  • Make sure to put path in Path in Environment Variables
  • Had to install Easy_Install (http://sourceforge.net/projects/pywin32/files/)
  • Make sure the Python Scripts path is also in Environment Variables
  • And get this... Had to go to MSDN and download full version of Visual Studio 2008 and only install C++ with the 64-bit option turned on (so Easy_Install could compile the Mercurial module for Python), but I could not get it to work with the Express edition because could not find the 64-bit option.
  • 必须单独安装 Python27 ( http://www.python.org/)
  • 确保将路径放在环境变量中的路径中
  • 必须安装 Easy_Install ( http://sourceforge.net/projects/pywin32/files/)
  • 确保 Python 脚本路径也在环境变量中
  • 得到这个......不得不去MSDN并下载完整版的Visual Studio 2008并且只安装C++并打开64位选项(所以Easy_Install可以为Python编译Mercurial模块),但我无法得到它使用 Express 版本,因为找不到 64 位选项。

Steps

脚步

  • In Command Console (after everything is installed)
    • C:\easy_install mercurial
  • 在命令控制台中(安装完所有内容后)
    • C:\easy_install mercurial

(If you installed everything you should see it download and compile the Mercurial module)

(如果你安装了所有东西,你应该会看到它下载并编译 Mercurial 模块)

  • In Git Console
    • Follow steps by @mauvis-ledford and @benno (Thanks guys!!!)
  • 在 Git 控制台中
    • 按照@mauvis-ledford 和@benno 的步骤操作(谢谢大家!!!)

回答by bolinfest

Here is a simple example of how to export your local clone of a Mercurial repo to GitHub.

这是一个简单的示例,说明如何将 Mercurial 存储库的本地克隆导出到 GitHub。

First, add the hggitextension to your ~/.hgrcfile if you have not already done so:

首先,如果您还没有将hggit扩展名添加到您的~/.hgrc文件中:

[extensions]
hggit =

Then run the following commands from the root of your Mercurial repository:

然后从 Mercurial 存储库的根目录运行以下命令:

hg bookmark -r default master
hg push -f git+ssh://[email protected]/bolinfest/plovr.git

Personally, I had to specify -fwhen running hg push, though I suspect it is because I had some complicated branches in my Mercurial repo. You may not find it necessary for your migration.

就个人而言,我必须-f在运行时指定hg push,尽管我怀疑这是因为我的 Mercurial 存储库中有一些复杂的分支。您可能认为迁移没有必要。

Make sure that you have the git+ssh:URI to your GitHub repo rather than the git:URI. (The git+ssh:URI is a bit harder to find in the new GitHub UI.)

确保您拥有git+ssh:GitHub 存储库的git:URI而不是URI。(git+ssh:在新的 GitHub UI 中更难找到 URI。)

Depending on the size of your repo, running hg pushmay take awhile, so be patient. When you are done, you should be able to see your project on GitHub. In this example, my project was available at https://github.com/bolinfest/plovr.

根据你的 repo 的大小,运行hg push可能需要一段时间,所以要有耐心。完成后,您应该能够在 GitHub 上看到您的项目。在这个例子中,我的项目在https://github.com/bolinfest/plovr 上可用。

As you would expect, now anyone can clone your public GitHub repo as follows:

正如您所料,现在任何人都可以克隆您的公共 GitHub 存储库,如下所示:

git clone [email protected]:bolinfest/plovr.git

回答by Maurício C Antunes

As of today, git distribution includes a tool to convert mercurial repositories to git, hg-to-git. If you have a recent git version installed, it is there. Worked very well for me.

截至今天,git 发行版包含一个将 mercurial 存储库转换为 git, hg-to-git 的工具。如果您安装了最新的 git 版本,它就在那里。对我来说效果很好。

回答by Piper

Here are all the pieces put together for a migration on Windows.

以下是在 Windows 上进行迁移的所有部分。

Prerequisites

先决条件

  • Git
  • Mercurial or TortoiseHg
  • Python 2.7 (3.x won't work)
  • 吉特
  • Mercurial 或 TortoiseHg
  • Python 2.7(3.x 不起作用)

During install, allow binding to .sh files.
Ensure that all tools are available in the PATH environment variable.

在安装过程中,允许绑定到 .sh 文件。
确保所有工具都在 PATH 环境变量中可用。

Migration

移民

  1. Open cmd.exe
  2. mkdir c:\git_work
  3. cd c:\git_work
  4. git clone http://repo.or.cz/r/fast-export.git
    This creates folder: c:\git_work\fast-export\
  5. Now you need mercurial libs for python. Either get them from hereordo the following steps.
    Edit c:\git_work\fast-export\hg-fast-export.py:

    import sys # move this line up here
    # Add a new line that imports [mercurial libraries][2] from this zip:
    sys.path.append(r'C:\Program Files\TortoiseHg\library.zip')
    # ...above the old line:
    from mercurial import node
    
  6. mkdir MyNewGitRepo

  7. Copy content of fast-export to MyNewGitRepo, ignore .git*
  8. hg-fast-import.sh -r c:\Path\To\MyOldHgRepo
  9. If this fails with "Error: repository has at least one unnamed head..." call the last line with parameter: --force
  10. Remove the migration scripts:

    git clean -n # see what would happen
    git clean -f # delete migration files
    
  11. Restore missing files by resetting the index and working tree.

    git reset --hard
    
  1. 打开cmd.exe
  2. mkdir c:\git_work
  3. cd c:\git_work
  4. git clone http://repo.or.cz/r/fast-export.git
    这将创建文件夹:c:\git_work\fast-export\
  5. 现在你需要 python 的 mercurial 库。从这里获取它们执行以下步骤。
    编辑 c:\git_work\fast-export\hg-fast-export.py:

    import sys # move this line up here
    # Add a new line that imports [mercurial libraries][2] from this zip:
    sys.path.append(r'C:\Program Files\TortoiseHg\library.zip')
    # ...above the old line:
    from mercurial import node
    
  6. mkdir MyNewGitRepo

  7. 将快速导出的内容复制到 MyNewGitRepo,忽略 .git*
  8. hg-fast-import.sh -r c:\Path\To\MyOldHgRepo
  9. 如果这失败并显示“错误:存储库至少有一个未命名的头...”调用带有参数的最后一行:--force
  10. 删除迁移脚本:

    git clean -n # see what would happen
    git clean -f # delete migration files
    
  11. 通过重置索引和工作树来恢复丢失的文件。

    git reset --hard
    

回答by Mike Atkisson

If you happen to be using GitHub andyour Mercurial repo is available via HTTP/HTTPS...

如果您碰巧在使用 GitHub并且您的 Mercurial 存储库可通过 HTTP/HTTPS 访问...

In the course of using fast-import (a great tool, that converted my repo without issue) and pushing my new git repo from my dev box to GitHub, I noticed an option to "Import Code from Another Repository" during the "Quick Setup" portion of my git repo on GitHub.

在使用快速导入(一个很棒的工具,它可以毫无问题地转换我的存储库)并将我的新 git 存储库从我的开发箱推送到 GitHub 的过程中,我注意到在“快速设置”期间有一个“从另一个存储库导入代码”的选项" 我在 GitHub 上的 git 存储库的一部分。

I tested it out and achieved the same results as using fast-import. Using the GitHub import option turned out to be a bit easier in my case than fast-import (again, nothing against that tool, and no, I don't work for GitHub). The steps which worked for me were:

我测试了它并获得了与使用快速导入相同的结果。在我的情况下,使用 GitHub 导入选项比快速导入要容易一些(同样,没有反对该工具,不,我不为 GitHub 工作)。对我有用的步骤是:

  1. Create a new GitHub repo
  2. Choose the "Import Code" option in Quick Setup once the repo is created
  3. Enter the URL of a publicly available hg, svn, or tfs repo and press "Begin import" in the "Github Importer" dialog
  1. 创建一个新的 GitHub 存储库
  2. 创建存储库后,在快速设置中选择“导入代码”选项
  3. 输入公开可用的 hg、svn 或 tfs 存储库的 URL,然后在“Github 导入器”对话框中按“开始导入”

回答by Ry4an Brase

If you're really only looking to do it this once you can use hg exportlike this:

如果您真的只想这样做一次,您可以hg export像这样使用:

hg export 0:tip -o all-changesets-in-one.patch

or if git prefers only one patch per file you can create one per changeset like this:

或者,如果 git 只喜欢每个文件一个补丁,您可以像这样为每个变更集创建一个:

hg export 0:tip -o changeset-%r.patch

presumably git applycan take one or the other of those formats.

大概git apply可以采用这些格式中的一种或另一种。

回答by Nowhere man

Have you tried tailor? It can pretty much convert anything to anything, as VCS go, and is even able to do it incrementally (that is, you convert once, then add new commits from the source to the target, without reconverting from scratch). You may not need it now, but it still is a nice feature.

你试过裁缝吗?它几乎可以将任何东西转换为任何东西,就像 VCS 一样,甚至可以增量地进行(也就是说,您转换一次,然后将新的提交从源添加到目标,而无需从头开始重新转换)。您现在可能不需要它,但它仍然是一个不错的功能。

回答by Remi Despres-Smyth

I tried some of the other answers here and didn't have much luck. Here is the pretty straightforward method that what worked for me, after much trial and error:

我在这里尝试了其他一些答案,但运气不佳。经过多次反复试验,这是对我有用的非常简单的方法:

First, download the hggitextension and add it to your ~/.hgrc or mercurial.ini file if you have not already done so:

首先,下载 hggit扩展并将其添加到您的 ~/.hgrc 或 mercurial.ini 文件(如果您还没有这样做的话):

[extensions]
hggit=\path\to\hg-git

For each branch you want to bring over to Git, add an Hg bookmark to the tip of the branch:

对于您想要引入 Git 的每个分支,在分支的顶端添加一个 Hg 书签:

hg bookmarks hg-default

Then use hggit to create a git repo in the same folder as your current Hg repo by running hg gexport. A .git folder will have been added as a result. When you then delete the .hg folder from your repo, you will be left with a local git repo.

然后使用 hggit 通过运行在与当前 Hg 存储库相同的文件夹中创建一个 git 存储库hg gexport。结果将添加一个 .git 文件夹。当你从你的 repo 中删除 .hg 文件夹时,你将留下一个本地 git repo。

回答by CAD bloke

An easy way to convert between Git & Hg with no local process is to open a free KilnHg / Fogbugz account, create a private repo, push one format to it then pull in the other format, it works with both Git & Hg and (should) do seamless conversions.

在没有本地进程的情况下在 Git 和 Hg 之间进行转换的一种简单方法是打开一个免费的 KilnHg / Fogbugz 帐户,创建一个私有存储库,将一种格式推送给它然后拉入另一种格式,它适用于 Git 和 Hg 并且(应该) 进行无缝转换。