107

I've been developing a java application using git as source code repository. I'd like to share the project with other java developers and hg seems to be most used by them.

My question is how do I convert a git repository to hg?

If I tried googling "convert git to hg" and every search hit is about converting from hg to git. I'm also using TortoiseHg.

Spoike
  • 119,724
  • 44
  • 140
  • 158
  • 16
    If you need to convert from mercurial to git instead: http://stackoverflow.com/questions/883452/git-interoperability-with-a-mercurial-repository – Christian Oudard Dec 15 '09 at 15:27
  • That last paragraph does not make sense. A typo? – tshepang Jun 16 '12 at 09:04
  • 1
    @Tshepang: I wanted "git -> hg" but got "hg -> git". I'll edit this question but I guess people got it anyway since there are answers now. – Spoike Jun 18 '12 at 06:18

9 Answers9

82

The hg convert utility isn't on by default after installation. In order to set it as such add the following to your .hgrc file.

[extensions]
hgext.convert=

If you're using TortoiseHg on Windows then this file resides in your home directory as mercurial.ini. After this setting change you will be able to use the hg convert utility.

Spoike
  • 119,724
  • 44
  • 140
  • 158
  • 6
    On Ubuntu the system wide change can be made at /etc/mercurial/hgrc – Sentient Jan 11 '13 at 22:39
  • 2
    On Windows, using TortoiseHg, it can be enabled by using the Global Settings menu item from the TortoiseHg shell extension menu in the Extensions section under Global Settings. – Umar Farooq Khawaja May 02 '14 at 12:22
  • The description is somewhat incomplete as it doesn't specify that hg will need access to git for the conversion or else you get the error "cannot find required "git" tool". – jmd Jun 22 '16 at 08:58
42

By using the Mercurial Convert extension

Add the following lines to your .hgrc or to enable the extension :

[extensions]
hgext.convert=

And typing a

cd src
hg convert --datesort . dst

(even though it can lead to some issues...)

Note:: metrix reports that this might not work:

     hg convert --datesort src dst

Note: there happens to be a bug with hg convert if you perform this command from a directory other than the source directory.
You will get the following error:

abort: cannot read tags from git-repo4/.git

Confirmed with git 1.7.9 and Mercurial 2.6.2 on Windows XP

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If that answer included the `.hgrc`setup instead of linking to it it would be comprehensive. – fbmd Mar 11 '15 at 09:30
17

You may want to look at the http://hg-git.github.com/ utility: a Git plugin for Mercurial

This plugin is originally developped by the guys of GitHub, and allows the convert from git<->mercurial losslessly. In theory, you could even be able to clone the hg repository.

vdboor
  • 21,914
  • 12
  • 83
  • 96
  • 2
    This is a better solution than converting the repository. It would be a better answer too, if it had more detail. One line answers are next to useless. – naught101 May 25 '12 at 02:54
  • 1
    @naught101, thanks for the suggestion, I've added some more info here to read directly. – vdboor May 25 '12 at 12:12
  • after you install & set up hg-git use hg gexport & hg gimport to convert between them. – CAD bloke Dec 04 '12 at 04:47
9

Distributed revision control with Mercurial states:

The revision control tools supported by convert are as follows:

  • Subversion
  • CVS
  • Git
  • Darcs

So maybe you don’t need any additional tool?

Bombe
  • 81,643
  • 20
  • 123
  • 127
6

The convert extension may work for you, but if it doesn't, you might want to check out tailor which is a general purpose tool for converting among the various distributed SCMs.

Luke Francl
  • 31,028
  • 18
  • 69
  • 91
2

New player in town: git-remote-hg. It provides native bridging support in git.

Just:

hg init ../hgrepo
git push hg::$PWD/../hgrepo
FelipeC
  • 9,123
  • 4
  • 44
  • 38
2

You could just not convert it at all. Just use the Hg-Git plugin! No one will know the difference.

Jay Atkinson
  • 3,279
  • 2
  • 27
  • 41
1

In the Git folder with all my Git repositories I created the Git2Hg.cmd file with the following content:

set PATH=%PATH%;C:\Program Files\TortoiseHg
set PATH=%PATH%;C:\Program Files\Git\bin
hg convert %1

Now I can convert a Git repository to Hg by typing this in a CMD:

Git2Hg "Folder Name"
stenci
  • 8,290
  • 14
  • 64
  • 104
0

Hg-Git Plugin or if u use for windows or migration from one to another visit here 2[Windows GIT]

dilshad
  • 734
  • 1
  • 10
  • 27