Git interoperability with a Mercurial Repository

Viewed 47310

I use GIT on a Mac. Enough said. I have the tools, I have the experience. And I want to continue to use it. No wars here...

The problem is always with interoperability. Most people use SVN, which is great for me. Git SVN works out of the box, and is a no frills solution. People can continue happily use SVN and I don't lose my workflow and neither my tools.

Now... Some guys come along with Mercurial. Fine for them: they have their reasons. But I can't find any GIT HG out-of-the-box. I don't want to switch to HG, but I still need to interoperate with their repository.

Any of you guys know a simple solution for this?

11 Answers

You should be able to use hg-git.

hg clone <hg repository>

edit ~/.hgrc and add :

[extensions]
hgext.bookmarks =
hggit =

create a bookmark so you will have a master in git :

cd <repository>
hg bookmark -r default master

edit .hg/hgrc in the repository and add :

[git]
intree = true

now you can create the git repository :

hg gexport

and you can use the resulting directory as a git clone. pulling from mercurial would be :

hg pull
hg gexport

and pushing to mercurial :

hg gimport
hg push

(Yes, you need to use hg with this workflow but your hacking will be all in git)

P.S. If you have a problem with this workflow, please file a bug.

You can try hg2git, which is python script and is part of fast-export, which you can find at http://repo.or.cz/w/fast-export.git .

You'll need to have mercurial installed though.

Since hg-git is a two-way bridge, it will also allow you to push changesets from Git to Mercurial.

The up-to-date answer in 2021 seems to be git cinnabar, which is used e.g. by Firefox.

Other scripts suggested on this page are either unmaintained or require outdated software to run (typically python 2.7). By contrast, cinnabar works with python 3.5+ and is maintained until the date of writing at least.

Once cinnabar installed, you can either clone mercurial repositories directly by prefixing with hg:::

git clone hg::https://hg.mozilla.org/mozilla-unified

Or add or set a remote to

git remote set-url origin hg::https://hg.mozilla.org/mozilla-unified
git fetch origin

You can push to mercurial remotes.

Using hg2git and git2hg commands allow to translate git commit sha1s to mercurial changeset and back. More docs available on the github page.

Related