Converting a Mercurial (hg) repository to Git on Windows (7)
Asked Answered
L

6

26

I'm really quite frustrated at this point. I have an existing hg repository that has months of coding history in it, and I want to get this into a private Github repository so I can work on it from there. Unfortunately I use Windows and I can't seem to find any way to convert the repository that I can actually get to work properly. Practically every bit of help and advice I can find online seems to assume I'm running from some kind *nix shell, which I'm not.

I currently have installed: TortoiseHG, Python 3.1 and Git for Windows.

What I've tried:

  1. hg-init plugin for hg - When I try to use the gexport command (and others from hg-git), I just get strings of python exceptions. I totally can't get git to even recognise the gremote command. I tried following what was here: http://github.com/blog/439-hg-git-mercurial-plugin

  2. hg-to-git (fast-export) - All tutorials keep expecting me to run a .sh file. Another stackoverflow thread seemed to imply that Powershell can run .sh files, but I can't make it run them for the life of me. Example: http://hivelogic.com/articles/converting-from-mercurial-to-git

  3. Mercurial to git converter - http://permalink.gmane.org/gmane.comp.version-control.git/36601 - This one doesn't want to work when I run it via Python, which complains that it has a syntax error on line 44. I don't know python, so I've got no idea.

Four hours of Google searching and wading through useless tutorials and people-more-fortunate-than-me singing the praises of each supposedly-helpful blog post, but I haven't gotten anywhere with this. Can anyone offer any assistance or suggestions for me to try?

Lascivious answered 16/7, 2010 at 17:2 Comment(1)
Here is the step-by-step tutorial.Cantharides
S
19
  1. Install Cygwin (with python, hg, git)
  2. Install git-hg
  3. run git-hg clone ...url...

Or tell me what the url is for this hg repo of yours, and I can convert it for you.

Scansion answered 17/7, 2010 at 1:20 Comment(7)
Thankyou, you rock. This Cygwin thing is kinda cool actually! Now I can treat windows sorta like Linux. To clarify, after installing cygwin, I then followed the instructions at hivelogic.com/articles/converting-from-mercurial-to-git - I had some trouble before I discovered that I had to merge any stray heads. Once I was down to a single head, the conversion worked just fine.Lascivious
Np, I'm glad you were able to convert to git =]Scansion
Firewall at work prevented "git clone git://repo.or.cz/fast-export.git" command from working, download archive from here: repo.or.cz/w/fast-export.git/shortlog/refs/heads/master.Munsey
Also, I was getting "IOError: [Errno 32] Broken pipe" before the conversion completed. I downloaded an older version and it worked fine.Munsey
However, it seems that hg-fast-export is currently broken on windows. See stackoverflow.com/questions/10020325 for the fix.Viborg
Syntax errors in the shell script: ./git-hg.sh: line 14: syntax error near unexpected token `elif Plus two others.Edrei
There is a good step by step here in a duplicate question: https://mcmap.net/q/537072/-how-can-i-convert-a-mercurial-repository-to-git-in-windows-duplicateAlaska
R
7

There is a lot of conflicting information about how to convert Mercurial repositories to Git on Windows. It really is pretty easy and only requires TortoiseHg and Git to be installed.

Preparation

Enable hg-git by adding the following to your Mercurial config file (%Userprofile%\mercurial.ini):

[extensions]
hggit = 
hgext.bookmarks =

[git]
intree = True

Conversion

Navigate to the repository directory (that contains the .hg subdirectory) on the command line and execute the following commands:

hg bookmark -r default master
hg gexport --debug
git config --bool core.bare false
git reset HEAD -- .

More information

You can find a detailed description of these steps in my blog article on the subject.

Roseline answered 4/6, 2015 at 14:44 Comment(2)
I assume you also need to have Hg itself installed, right? (You didn't mention that but perhaps you assumed it would be installed already)Wilding
@ClayNichols Hg comes with TortoiseHg.Intonation
G
2

Have you considered installing msysgit? The git bash shell it gives you should be able to run .sh files.

Gourde answered 16/7, 2010 at 17:6 Comment(4)
If I don't know anything about bash, could I run into trouble?Lascivious
As long as you go carefully, you should be okay - plus, picking up at least some rudimentary linux command-line skills is probably a good thing to do for any developer.Gourde
+1 to cancel out the negatives. Seems like a valid answer to meCrispas
I installed msysgit and ran bash, but the script still won't run because it can't import the Mercurial module, and I'm not sure how to resolve that.Lascivious
T
2

Everything I've read about these Hg converters for Git does point out one obvious error you've made: Using Python 3!

Those Python errors you've been getting are due to scripts like hg-git and fast-export being built using Python 2.6. Python 3 treats "print" differently, hence your errors.

Either rewrite the scripts to work with your version or install Python 2.6.

Make sure to add it to your PATH setting. Same goes for when you install msysGit. Be sure you choose the second option at install to add Git to your path.

You also need Mercurial For Python, available at TortoiseHG's thg-winbuild project:

https://bitbucket.org/tortoisehg/thg-winbuild/downloads

The file will be named 'mercurial-2.2.2.win-amd64-py2.6.exe' for 64-bit Windows and 'mercurial-2.2.2.win32-py2.6.exe' for 32-bit. While you're at it, get the companion Mercurial 2.2.2 installer, just in case.

Although you stated you don't wish to install yet another VCS software, using any of these Hg converters requires Mercurial to also be installed.

Another pitfall to look out for is these converters may not work with the latest versions of Mercurial. You might need to experiment a bit with older releases to find one that works well with the scripts.

Be sure to carefully read through the tutorials and the Readme files for whichever converter you settle upon using. It seems a lot of your grief and trouble stems from having impatiently jumped into the deep end after only skimming the documentation.

There are patches available that add Mercurial support to Git and msysGit, but you'll need to be comfortable with patching and compiling from source if you wish to follow that route.

Thoughtless answered 11/4, 2014 at 7:21 Comment(1)
when i've faced this problem it was python 2.7. Direct links for x64: python.org/download/releases/2.7 and bitbucket.org/tortoisehg/files/downloads/…Byzantium
M
0

It might seem silly (or a solution for the desperate...) but I think that Bazaar has good support for both Hg and Git repositories, and it works well on Windows.
So if all else fails, maybe you can use bzr-hg and bzr-git to make the conversion...

Mcintosh answered 16/7, 2010 at 17:17 Comment(1)
Possibly, I'd prefer to not have to install yet another source control system though...Lascivious
P
0

If the original Mercurial repo is available on the web, and if you don't mind using GitHub to do this, converting from Mercurial to Git is now trivially easy:

  1. Log in to GitHub.
  2. Click the + in the upper-right.
  3. Select Import repository.
  4. Supply the Mercurial repository's URL, a name for it under your account, and whether it'll be public or private.
  5. Click Begin import and wait.

You don't even need to stay on the page after that. After a while, GitHub will notify you by e-mail that the import is finished.

Preferment answered 6/2, 2019 at 13:29 Comment(1)
This is exactly the solution I needed, so, I know it is not really a response to the OP's question, but thank you a lot anyway !Rockbound

© 2022 - 2024 — McMap. All rights reserved.