299

I need to convert a mercurial project to a git project, but I would like to keep the commit history intact. My current solution was to just remove hg related files and then git init && add manually the files I needed, but that would not keep the history. Are there any solutions to this?

7
  • 1
    A git-hg utility for checking out and tracking a mercurial repo. github.com/offbytwo/git-hg
    – MGP
    Apr 16, 2013 at 13:11
  • 2
    You might also try Kiln Harmony which keeps both a git and mercurial version of a repo and syncs everything, so it's usable from both.
    – Wilbert
    Apr 16, 2013 at 13:26
  • 2
    Better approach github.com/buchuki/gitifyhg
    – sorin
    Mar 25, 2015 at 18:21
  • 3
    @Ian and others. Its not actually a duplicate question, the other question is about migrating a specific folder, this one is about the entire repo. Although answers to this questions have been posted to that question, This is a different question. It should be marked as a related question.
    – simgineer
    Jul 6, 2015 at 2:45
  • 4
    For reference sake, Kiln Harmony has been discontinued. Oct 26, 2017 at 14:33

8 Answers 8

298

You can try using fast-export:

cd ~
git clone https://github.com/frej/fast-export.git
git init git_repo
cd git_repo
~/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD

Also have a look at this SO question.


If you're using Mercurial version below 4.6, adrihanu got your back:

As he stated in his comment: "In case you use Mercurial < 4.6 and you got "revsymbol not found" error. You need to update your Mercurial or downgrade fast-export by running git checkout tags/v180317 inside ~/fast-export directory.".

20
  • 8
    As an additional note, you can also pass in -A with an authors map file if you need to map Mercurial authors to Git authors. Jun 12, 2013 at 2:15
  • 12
    n.b. Install Mercurial before executing Step 5 if you haven't already. mercurial.selenic.com
    – Kevin Zych
    Jun 18, 2013 at 2:06
  • 7
    If you have errors like "repository has at least one unnamed head", you can specify --force option to deal with broken tree.
    – iurii
    May 21, 2014 at 14:23
  • 19
    doesn't work out of the box for windows. therefore may as well not work for 97% of people - might be worth mentioning that you can use cygwin or similar in that case (even then it has hidden dependencies)
    – jheriko
    Jan 12, 2015 at 17:26
  • 7
    @jheriko doesn't work out of the box for windows. therefore may as well not work for 97% of people - That number (97%) you're mentioning is from 2005. Dec 9, 2016 at 17:03
104

Ok I finally worked this out. This is using TortoiseHg on Windows. If you're not using that you can do it on the command line.

  1. Install TortoiseHg
  2. Right click an empty space in explorer, and go to the TortoiseHg settings:

TortoiseHg Settings

  1. Enable hggit:

enter image description here

  1. Open a command line, enter an empty directory.

  2. git init --bare .git (If you don't use a bare repo you'll get an error like abort: git remote error: refs/heads/master failed to update

  3. cd to your Mercurial repository.

  4. hg bookmarks hg

  5. hg push c:/path/to/your/git/repo

  6. In the Git directory: git config --bool core.bare false (Don't ask me why. Something about "work trees". Git is seriously unfriendly. I swear writing the actual code is easier than using Git.)

Hopefully it will work and then you can push from that new git repo to a non-bare one.

8
  • 3
    The push kept failing for me on the push with error "dulwich.errors.RefFormatError: refs/heads/ref". It turns out that I had several bookmarks. After deleting each bookmark, it stopped failing. Jun 30, 2016 at 0:25
  • 11
    Per the hg-git documentation, "TortoiseHg comes with hg-git" and just needs to be enabled (as shown above). However, it is not included with the command line version of mercurial (It can be installed if desired). If you have both TortoiseHg & HG installed, you need to be sure the hg.exe from TortoiseHg is used, and not the one form the command line installation. If you get the error No module named hggit, try fully qualifying the hg command: "C:\Program Files\TortoiseHg\hg" push c:/path/to/your/git/rep
    – Javaru
    Aug 28, 2016 at 14:23
  • 2
    Note that this method only seems to import the currently checked-out branch (I'm looking for a solution that will import the whole repository).
    – robyaw
    Apr 4, 2017 at 9:06
  • 12
    Wouldn't hg bookmark -r default master be better than hg bookmarks hg?
    – Heikki
    Nov 1, 2017 at 14:01
  • 7
    For the files to appear in the repo, do: git checkout hg
    – user984003
    Aug 29, 2019 at 13:15
82

If you want to import your existing mercurial repository into a 'GitHub' repository, you can now simply use GitHub Importer available here [Login required]. No more messing around with fast-export etc. (although its a very good tool)

You will get all your commits, branches and tags intact. One more cool thing is that you can change the author's email-id as well. Check out below screenshots:

enter image description here

enter image description here

8
  • Does it also migrate issues and wikis?
    – thomthom
    Apr 3, 2016 at 16:59
  • @thomthom No only commits, branches & tags
    – atulkhatri
    Apr 3, 2016 at 17:22
  • 3
    You need a paid plan on Github to create private repositories. Not a problem if what you're converting is a public source tree anyway, of course.
    – Åsmund
    Jun 30, 2016 at 15:04
  • @Åsmund one could also use the tool, clone the git, then delete the public repo, if one is only worried about public exposure of the code when it is not ready
    – Tommy
    Sep 1, 2016 at 1:46
  • 5
    Do you know how to upload and convert a local mercurial repository? Dec 7, 2016 at 20:57
21

Some notes of my experience converting Mercurial to Git.

1. hg-fast-export

Using hg-fast-export failed and I needed --force as noted above. Next I got this error:

error: cannot lock ref 'refs/heads/stable': 'refs/heads/stable/sub-branch-name' exists; cannot create 'refs/heads/stable'

Upon completion of the hg-fast-export I ended up with an amputated repo. I think that this repo had a good few orphaned branches and that hg-fast-export needs a somewhat idealised repo. This all seemed a bit rough around the edges, so I moved on to Kiln Harmony (http://blog.fogcreek.com/announcing-kiln-harmony-the-future-of-dvcs/)

2. Kiln

Kiln Harmony does not appear to exist on a free tier account as suggested above. I could choose between Git-only and Mercurial-only repos and there is no option to switch. I raised a support ticket and will share the result if they reply.

3. hg-git

The Hg-Git mercurial plugin (http://hg-git.github.io/) did work for me. FYI on Mac OSX I installed hg-git via macports as follows:

  • sudo port install python27
  • sudo port select --set python python27
  • sudo port install py27-hggit
  • vi ~/.hgrc

.hgrc needs these lines:

[ui]
username = Name Surname <[email protected]>

[extensions]
hgext.bookmarks =
hggit = 

I then had success with:

hg push git+ssh://[email protected]:myaccount/myrepo.git

4. Caveat: Know your repo

All the above are blunt instruments and I only pushed ahead because it took enough time to get the team to use git properly.

Upon first pushing the project per (3) I ended up with all new changes missing. This is because this line of code must be viewed as a guide only:

$ hg bookmark -r default master # make a bookmark of master for default, so a ref gets created

The theory is that the default branch can be deemed to be master when pushing to git, and in my case I inherited a repo where they used 'stable' as the equivalent of master. Moreover, I also discovered that the tip of the repo was a hotfix not yet merged with the 'stable' branch.

Without properly understanding both Mercurial and the repo to be converted, you are probably better off not doing the conversion.

I did the following in order to get the repo ready for a second conversion attempt:

hg update -C stable
hg merge stable/hotfix-feature
hg ci -m "Merge with stable branch"
hg push git+ssh://[email protected]:myaccount/myrepo.git

After this I had a verifiably equivalent project in git, however all the orphaned branches I mentioned earlier are gone. I don't think that is too serious, but I may well live to regret this as an oversight. Therefore my final thought is to keep the original anyway.

Edit: If you just want the latest commit in git, this is simpler than the above merge:

hg book -r tip master
hg push git+ssh://[email protected]:myaccount/myrepo.git
2
  • 6
    Fog Creek responded to my support ticket confirming that Kiln Harmony is no longer available. Dec 14, 2015 at 14:56
  • blog.fogcreek.com shows SSL certificate error. I don't trust people who can not handle SSL properly and still do it. Aug 23 at 7:13
13

I had a similar task to do, but it contained some aspects that were not sufficiently covered by the other answers here:

  • I wanted to convert all (in my case: two, or in general: more than one) branches of my repo.
  • I had non-ASCII and (being a Windows user) non-UTF8-encoded characters (for the curious: German umlaute) in my commit messages and file names.

I did not try fast-export and hg-fast-export, since they require that you have Python and some Mercurial Python modules on your machine, which I didn't have.

I did try hg-init with TortoiseHG, and this answer gave me a good start. But it looked like it only converts the current branch, not all at once (*). So I read the hg-init docs and this blog post and added

[git]  
branch_bookmark_suffix=_bookmark

to my mercurial.ini, and did

hg bookmarks -r default master  
hg bookmarks -r my_branch my_branch_bookmark  
hg gexport

(Repeat the 2nd line for every branch you want to convert, and repeat it again if you should happen to do another commit before executing the 3rd line). This creates a folder git within .hg, which turns out to be a bare Git repo with all the exported branches. I could clone this repo and had a working copy as desired.

Or almost...

Running

git status

on my working copy showed all files with non-ASCII characters in their names as untracked files. So I continued researching and followed this advice:

git rm -rf --cached \*  
git add --all
git commit 

And finally the repo was ready to be pushed up to Bitbucket :-)

I also tried the Github importer as mentioned in this answer. I used Bitbucket as the source system, and Github did quite a good job, i.e. it converted all branches automatically. However, it showed '?'-characters for all non-ASCII characters in my commit messages (Web-UI and locally) and filenames (Web-UI only), and while I could fix the filenames as described above, I had no idea what to do with the commit messages, and so I'd prefer the hg-init approach. Without the encoding issue the Github importer would have been a perfect and fast solution (as long as you have a paid Github account or can tolerate that your repo is public for as long as it takes to pull it from Github to your local machine).


(*) So it looked like before I discovered that I have to bookmark all the branches I want to export. If you do and push to a bare (!) repo, like the linked answer says, you get all the branches.

3
  • 1
    to solve encoding problem with hg-fast-export (not sure for github importer) you can pass parameter 'e' like this: -e cp1251
    – Pavel
    May 7, 2018 at 14:38
  • to solve file names encoding problem with hg-fast-export you can use '--fe' option like --fe cp1251
    – Sandre
    Oct 4, 2019 at 15:26
  • "Repeat the 2nd line for every branch you want to convert"?! Really?! I have a big old repository with MANY feature branches that all should be kept... Aug 23 at 7:18
10

From:

http://hivelogic.com/articles/converting-from-mercurial-to-git

Migrating

It’s a relatively simple process. First we download fast-export (the best way is via its Git repository, which I’ll clone right to the desktop), then we create a new git repository, perform the migration, and check out the HEAD. On the command line, it goes like this:

cd ~/Desktop
git clone git://repo.or.cz/fast-export.git
git init git_repo
cd git_repo
~/Desktop/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD

You should see a long listing of commits fly by as your project is migrated after running fast-export. If you see errors, they are likely related to an improperly specified Python path (see the note above and customize for your system).

That’s it, you’re done.

2
  • 2
    This worked for me, but I had to add a step. fast-export broke for all but the most recent versions of Mercurial (wasn't available through apt) so you either have to manually install a shiny new Mercurial >4.6 or just downgrade fast-export by adding a git checkout 19aa906 as a new step 3 above to get to the last commit that worked
    – Paul K
    Aug 13, 2019 at 14:29
  • hivelogic.com doesn't exist anymore. Aug 24 at 9:57
6

Another option is to create a free Kiln account -- kiln round trips between git and hg with 100% metadata retention, so you can use it for a one time convert or use it to access a repository using whichever client you prefer.

4
  • 6
    How do you use kiln for converting? Nov 4, 2014 at 8:40
  • Kiln repositories are usable by both git and hg. So you can import from one client, and then export from the other client, as many times as you like. Mar 11, 2015 at 9:22
  • 4
    Kiln Harmony was very nice while it lasted, but it was shut down in September 2016. (And Kiln's free tier is being shut off next month.)
    – Josh
    Apr 7, 2017 at 0:38
  • 1
    Kiln was sold recently and has had a lot of downtime since. Their status pages don't reflect most of this, but it's about once / month. Would only recommend if you absolutely need Mercurial and can't find another host.
    – Josh Noe
    Nov 27, 2019 at 20:09
2

This would be better as a comment, sorry I do not have commenting permissions.

@mar10 comment was the missing piece I needed to do this.

Note that '/path/to/old/mercurial_repo' must be a path on the file system (not a URL), so you have to clone the original repository before. – mar10 Dec 27 '13 at 16:30

This comment was in regards to the answer that solved this for me, https://stackoverflow.com/a/10710294/2148757 which is the same answer as the one marked correct here, https://stackoverflow.com/a/16037861/2148757

This moved our hg project to git with the commit history intact.

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