8

I'd like to fork a Git repository, convert it to Mercurial, and contribute my changes back to the original Git repository when I'm done. I am more concerned with a safe and stable conversion process than its convenience. I will be pulling changes from Git into Mercurial on a regular basis but rarely contributing any changes back.

I'm not comfortable using hg-git because many of the bugs reported against the project have gone unanswered for years. I suspect it's safer to use hg convert to convert Git to Hg than using hg-git.

My question is: say I've already converted the repository to Mercurial and made some changes, how do I contribute these changes back to the official repository? I'd like to contribute my changes back to the official Git repository without losing any history information (that is, I don't want to fold multiple changesets into a one).

What is the easiest and safest way to do this?

Gili
  • 86,244
  • 97
  • 390
  • 689
  • 2
    There are reasons to use a modern scm when a project is officially hosted on something like svn or cvs, thus the wide availability of tools to make that easy. But if the project is already hosted in a modern scm then there's a lot less reason not to just use the same scm locally. – bames53 Jun 26 '12 at 14:36
  • @bames53, I agree but like many other decisions we make on a daily basis, this one is purely subjective :) – Gili Jun 26 '12 at 14:40
  • 1
    Just to be sure, you know that link isn't to the official hg-git repo? That can be found [here](https://github.com/schacon/hg-git). – obmarg Jun 26 '12 at 17:38
  • 2
    @obmarg, according to http://hg-git.github.com/ the link you provided is actually a mirror of the link I provided (which is the canonical source). See the links at the bottom of the website. – Gili Jun 26 '12 at 18:02

1 Answers1

8

You can try and export your Mercurial commits as patches:

hg export --git -r 1 >patch.diff

This .diff file should be recognized by Git and could be added to the git repo with git apply.
(This was suggested in "Convert a Mercurial Repository to Git", where the more up-to-date script hg-fast-export was also mentioned)

The --git option of hg export will make sure you generate diffs in the git extended diff format. See hg help diffs for more information.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Won't this collapse multiple changesets into one? – Gili Jun 26 '12 at 17:29
  • @Gili: if that is so, you would simply list all the Hg revisions you are interested in and, for each one, produce a `.diff` file (that you would apply in turn to your Git repo). `hg export` can take a single revision or a range of revision. – VonC Jun 26 '12 at 18:06
  • I ran a quick test: `hg export --git -r 1 -r 2 >patch.diff` embeds both revisions into the same file but provides sufficient information for which changes go into which changeset. I believe this will work. Thanks! – Gili Jun 26 '12 at 18:09
  • git does not accept such patches, says "error: unrecognized input" – Alex Jul 25 '17 at 20:30
  • @Alex Strange: what git version re you using? On which OS? Which git command did you use? Is there some eol issue? (\r\n applied to a \n file) – VonC Jul 25 '17 at 21:12
  • I opened the generated patch in a text editor and saved it as utf-8 without BOM, then git was able to read the patch. But unfortunately it accused other problems down the road. I don't remember exactly the error message, but other people suggested that git has problems with patches with binary files (at least with hg-generated patches with binary files) – Alex Jul 26 '17 at 14:12
  • @Alex That would explain the other problem indeed: I tested it with pure text diff, no binary involved. – VonC Jul 26 '17 at 14:13