47

I've already tried hg2git through fast-export and I've already tried 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.

hg-git simply failed with some libzip compression error.

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

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.

3
  • can't the line feeds be fixed afterwards?
    – tonfa
    Sep 7, 2009 at 14:06
  • Would be possible. But it can't be the right solution to have to send off the repo to someone using Unix to convert it. There must be some way on Windows.
    – Tigraine
    Sep 7, 2009 at 22:54
  • @Joschua .. Yes I gave up after some time trying this on Windows and ran the conversion in my Linux VM
    – Tigraine
    Jul 26, 2011 at 7:44

12 Answers 12

57

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

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
7
  • 4
    This works only for Mercurial 2.2. If you have Mercurial 2.3, then try installing an older Python package, e.g. pip install mercurial==2.2, otherwise you get ImportError: cannot import name repo
    – Andrei
    Aug 20, 2012 at 10:05
  • Thanks, this was exactly what I needed. For me this worked, hg v2.0.2, git v1.7.9.5, python v2.7.3
    – mikelong
    Nov 16, 2012 at 2:16
  • 3
    This worked for me on OSX using Mercurial 2.4+20121105 and git 1.7.10.2. Also, I cloned from https://github.com/frej/fast-export rather than the repo above.
    – ben.snape
    Apr 8, 2013 at 9:37
  • 10
    The instructions above are a little complicated; checking out fast-export into your new repo and then cleaning is a bit kludgy. Simpler would be: git clone git://repo.or.cz/fast-export.git; mkdir myrepo; cd myrepo; git init; ../fast-export/hg-fast-export.sh -r ../path/to/local/hg/repo. This way you don't need to worry about fast-export.git potentially clashing with files from your original repo.
    – benno
    Jun 30, 2013 at 7:01
  • 1
    FYI, when running hg-fast-export.sh I got the error message Error: Branch [master] modified outside hg-fast-export: which happened because I didn’t init into a clean git repository, but was trying to fast export into an existing repo. Don't do that
    – dpdearing
    Apr 6, 2014 at 17:04
14

Windows 7 64-bit Solution

How to convert a Mercurial repo into a Git repo.

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.

My Start Environment

  • Visual Studio 2010/2012
  • TortoiseHg 2.8.2; Includes 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

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.

Steps

  • In Command Console (after everything is installed)
    • C:\easy_install mercurial

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

  • In Git Console
    • Follow steps by @mauvis-ledford and @benno (Thanks guys!!!)
5
  • It is stressing me out just reading this well-organized post. Will try.
    – tofutim
    Mar 19, 2014 at 18:35
  • I needed ez_setup.py (missed step)? Now waiting for VS2008 to come down from MSDN. Sheesh...
    – tofutim
    Mar 19, 2014 at 19:07
  • 1
    Please update if I missed a step. This was from notes I took over the several weeks of trying to get this to work. Mar 19, 2014 at 22:17
  • You don't have to install the full Visual Studio package, you just have to install the correct version of the Visual C++ Redistributable package. The 2005 version was what wound up working for me. Mar 10, 2016 at 20:47
  • Did not work for me. And although link-only answers are not allowed, let's add a comment only, this one solved two weeks of my headaches: helgeklein.com/blog/2015/06/… and I just had to share it with all of you. Jun 11, 2019 at 17:38
8

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

First, add the hggit extension to your ~/.hgrc file if you have not already done so:

[extensions]
hggit =

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

hg bookmark -r default master
hg push -f git+ssh://git@github.com/bolinfest/plovr.git

Personally, I had to specify -f when 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.

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.)

Depending on the size of your repo, running hg push may 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.

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

git clone git@github.com:bolinfest/plovr.git
3
  • 2
    I got hggit to move data from a local Hg repo to a local git repo, but it looks like it moved all commits across into the master branch and didn't respect the Hg branches I had. Did I miss something? Jan 27, 2014 at 20:51
  • I think the hggit extension has been renamed to just git.
    – user975326
    Sep 5, 2015 at 7:12
  • 1
    No, it hasn't. Thankfully it looks like Remi figured out the problem, as she has the correct solution posted as another answer. Mar 10, 2016 at 20:49
7

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.

1
  • Thanks, that's the simplest way to convert mercurial repo to git. I tried other solutions above, always stuck with some error messages, such as complain core.ignoreCase set, repo is not local, etc.
    – BMW
    Aug 28, 2017 at 2:19
7

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

Prerequisites

  • Git
  • Mercurial or TortoiseHg
  • Python 2.7 (3.x won't work)

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

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 here or do 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\lib\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
  • This worked well. Before step 10 you should git checkout HEAD because the export doesn't do that automatically. Dec 2, 2016 at 17:36
6

If you happen to be using GitHub and your Mercurial repo is available via 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.

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:

  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
4

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

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:

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

presumably git apply can take one or the other of those formats.

3
  • Thanks, but unfortunately I didn't find a way to make git apply those patches and preserve the commit message.
    – Tigraine
    Sep 10, 2009 at 22:34
  • 1
    Really? Managing patches received by email is one of the things git is supposed to do well, and mercurial uses the git diff format, so I'd think with minimal massaging you could get the emailed patches applied. Sep 11, 2009 at 20:51
  • I also have to admit that I didn't really try all that hard. It kept reporting a whitespace error in the file. Seems like "hg export --diff" isn't the same patch format after all..
    – Tigraine
    Sep 12, 2009 at 17:26
2

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.

1
  • Well, tailor had stopped being actively maintained for a while, it's no surprise. It's a bit sad because it could have been a real swiss-army knife of VCS conversion. But if you work with git, you now have a bunch of plugins that add the ability to directly clone, pull and push to antoher VCS (there are at least Subversion, Mercurial and Bazaar). Jul 23, 2013 at 22:07
2

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 hggit extension and add it to your ~/.hgrc or mercurial.ini file if you have not already done so:

[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:

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.

1
  • 1
    Note that the new Mercurial URL is www.mercurial-scm.org. Dec 13, 2016 at 15:13
2

Not any more. Dead as a Dodo.

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.

3
  • After countless hours of hair pulling with fast-export, hg-git, python, cygwin and all the hopeless tutorials/errors, KlinHg did the trick painlessly. Thank You!!! And for everyone else struggling, it is not worth it. Just use KlinHg ;)
    – ruuter
    Jun 28, 2014 at 22:16
  • Sees to be not available any more Nov 21, 2021 at 6:25
  • Kiln shut down quite a few years ago. Bitbucket dropped Mercurial a while ago too.
    – CAD bloke
    Nov 21, 2021 at 6:26
0

There doesn't seem to be any reason you can't run hg2git on windows. It's python which has a windows port. Just make sure the proper libraries are there and run it on the window box.

1
  • No unfortunately not. I tried running the py scripts directly and I couldn't invoke them due to not being able to execute .sh on Win.. And I really don't want to install cygwin for this.
    – Tigraine
    Sep 9, 2009 at 9:37
0

If anyone is using Homebrew on OS X and wants to know the minor tweaks to get it to work properly please see the article here: http://www.ccannon.net/mercurial-to-git-conversion-with-homebrew.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.