Converting Mercurial folder to a Git repository

39,079

Solution 1

On Linux or anything with bash/sh or similar, or python, try with fast export:

cd
git clone git://repo.or.cz/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

Solution 2

Windows: TortoiseHG Hg-Git extension

Hg-Git can be used to convert a Mercurial repository to Git. You can use a local repository or a remote repository accessed via SSH, HTTP or HTTPS.

Example of local repositories conversion.

  1. Install Hg-Git.

    • On Windows, TortoiseHg comes with Hg-Git, though you need to enable it via the setting tool (in extensions section)

      TortoiseHg Settings

      or manually in ~/mercurial.ini

      [extensions]
      hggit =
      
  2. Use the following commands to convert the repository:

    $ mkdir git-repo; cd git-repo; git init; cd ..
    $ cd hg-repo
    $ hg bookmarks hg
    $ hg push ../git-repo
    

The hg bookmark is necessary to prevent problems as otherwise hg-git pushes to the currently checked out branch confusing Git. This will create a branch named hg in the Git repository. To get the changes in master use the following commands (only necessary in the first run, later just use git merge or rebase):

$ cd git-repo
$ git checkout -b master hg

Solution 3

You can (from Mercurial side):

  • using Convert extension with --filemap option convert part of original repo into smaller with only needed files|directories
  • with hg-git extension push stripped repo to Git

or (instead of hg-git), using Mercurial bridge in Git, clone|pull repository from Git

Solution 4

Gitify

Seems as a more modern and easy to use alternative to perform the conversion https://github.com/buchuki/gitifyhg

pip install gitifyhg
git clone gitifyhg::<hgrepoaddress>
# done, you have a git repo with the entire history of the hg one

Solution 5

Convert a Mercurial repository to Git on Windows 10

If no problem with encoding - use TortoiseHG Hg-Git extension

md new-repo && cd new-repo
git init --bare .git
cd ..\old-mercurial-repo
hg bookmark -r default master
hg push ..\new-repo
cd ..\new-repo
git config --bool core.bare false

If something wrong with encoding - use fast-export

Install Bash

Open PowerShell as Administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Install Ubuntu 16.04 LTS from Microsoft Store

Open Bash and run

install mercurial

sudo -s
apt-get update
apt install mercurial

get fast-export v180317 (at the moment versions after 180317 does not work correctly)

cd /mnt/c/path_to_work_folder
git clone https://github.com/frej/fast-export.git
cd fast-export
git checkout tags/v180317
cd ..

convert repository

git init new-repo && cd new-repo
git config core.ignoreCase false && git config core.quotepath off
../fast-export/hg-fast-export.sh -r ../path_to_mercurial_repo/ --fe cp1251
git checkout master

encoding options:

  • -f encoding, like -f cp1251
  • --fe file name encoding like --fe cp1251
Share:
39,079
Simone Carletti
Author by

Simone Carletti

Hi! My name is Simone Carletti. I'm a passionate programmer and scuba diving instructor. I'm the CTO at DNSimple. I created RoboWhois and RoboDomain. I have been involved with software development for more than a decade, contributing code and creating libraries.

Updated on January 23, 2020

Comments

  • Simone Carletti
    Simone Carletti over 4 years

    I don't have a huge experience with Mercurial, I'm mostly a Git guy.

    I would love to mirror a specific Mercurial folder/file in a git Repository. What I'm actually trying to do is to export the history of a file from a Mercurial repository to Git and being able to keep this in sync with future commits.

    Do you have any suggestion on how to proceed? I believe that the way to go should be to get the history of the Mercurial patch, periodically export every single commit as a patch and apply the Mercurial patches to the Git repository.

  • Simone Carletti
    Simone Carletti about 12 years
    I know about fast-export, but it doesn't solve my issue because it exports the entire repo and the original repo is more than 1.5GB.
  • Yohann
    Yohann about 12 years
    Ok, you want to keep git synchronize with mercurial ? Like this ?
  • Yohann
    Yohann about 12 years
    Or you can use Mercurial outgoing hook to push to git ?
  • Simone Carletti
    Simone Carletti about 12 years
    I have no control over the original HG repository, I need each new commit to be submitted also in the GIT repo by pulling the original HG repo.
  • mar10
    mar10 over 10 years
    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.
  • Artur Czajka
    Artur Czajka over 10 years
    In my case had to use PYTHON=python2 ~/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo. Apart from that, it worked flawlessly.
  • Marcus Westin
    Marcus Westin almost 10 years
    Thanks for concise answer! I had to sudo easy_install mercurial first. Would be nice to have in the instructions
  • Simone Carletti
    Simone Carletti over 9 years
    I didn't want to convert an entire repo, just a folder.
  • Andrei
    Andrei about 9 years
    Awesome! Beware of unicode error though github.com/buchuki/gitifyhg/pull/98
  • Timmmm
    Timmmm over 8 years
    I've found git-hg to be quite buggy, but this worked like a charm, and didn't require any additional downloads which is nice.
  • Simone Carletti
    Simone Carletti over 8 years
    I wanted a folder, not the entire repo.
  • Tommy
    Tommy almost 8 years
    github now has a tool that supports this directly from github, see stackoverflow.com/questions/16037787/…
  • naXa stands with Ukraine
    naXa stands with Ukraine over 7 years
    @SimoneCarletti Take a look at this question. It should help you to export a file/folder and its history in a temporary repository. Then you can use any of the provided solutions to convert the temporary Mercurial repository to Git.
  • Zoran Pavlovic
    Zoran Pavlovic almost 7 years
    Note - gitifyhg is currently badly out of date with the latest Mercurial versions.
  • Augustas
    Augustas over 6 years
    If you are on Windows machine use the Git bash and run hg-fast-export.sh
  • Throw Away Account
    Throw Away Account almost 5 years
    I had to run PYTHONPATH=/usr/lib/python2.7/dist-packages/:$PYTHONPATH because hg-fast-export depends on Mercurial as a library.
  • Augusto Destrero
    Augusto Destrero over 4 years
    If you are getting the error ImportError: cannot import name revsymbol the simplest solution is downgrading fast-export to v180317, that is cd ~/fast-export; git checkout v180317. More details here.
  • Augusto Destrero
    Augusto Destrero over 4 years
    I wrote a quick post on my blog with some more details to convert a Mercurial repository to Git. There are also some tips if you are using Bitbucket.
  • Brandon
    Brandon over 4 years
    @MarcusWestin is there a way to do this without that command? I don't have sudo permissions on my machine so I can't execute that to install the module "mercurial"
  • Vlad
    Vlad over 2 years
    The repo.or.cz site might be slow to respond. It also lives on github.com/frej/fast-export