Converting Mercurial folder to a Git repository

Converting Mercurial folder to a Git repository

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.

Solutions/Answers:

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):

Related:  Are DVCS like Git inappropriate for teams using continuous integration?
$ 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:

I wrote up exactly how to do this here: Push to GitHub using Mercurial. I have been using that technique to push several Mercurial repositories to GitHub for over a year, with no problems.

Solution 6:

On Windows can be a bit tricky. After enabling the right plugins in mercurial( hggit), the TortoiseHG can also be used.

  1. Clone mercurial repo
  2. Clone git repo
  3. Enable console :
    Enabling console
  4. Using the console :

    % hg bookmarks hg

    % hg push <relative path to>/<git-repo>

References