147

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.

3
  • 1
    Nowadays, GitHub.com will import it for you. Commented Jun 19, 2020 at 16:12
  • Related: a good walk-through on git-scm.com: git-scm.com/book/en/v2/… Commented Sep 5, 2020 at 16:12
  • @BradTurek Nope. Support for importing Mercurial, [...] repositories ended on April 12, 2024 Commented Nov 3, 2024 at 10:14

8 Answers 8

131

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 
Sign up to request clarification or add additional context in comments.

13 Comments

Ok, you want to keep git synchronize with mercurial ? Like 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.
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.
Thanks for concise answer! I had to sudo easy_install mercurial first. Would be nice to have in the instructions
If you are on Windows machine use the Git bash and run hg-fast-export.sh
|
69

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 

4 Comments

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.
I wanted a folder, not the entire repo.
github now has a tool that supports this directly from github, see stackoverflow.com/questions/16037787/…
@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.
13

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

Comments

11
+50

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 

3 Comments

I didn't want to convert an entire repo, just a folder.
Awesome! Beware of unicode error though github.com/buchuki/gitifyhg/pull/98
Note - gitifyhg is currently badly out of date with the latest Mercurial versions.
8

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

Comments

3

hg-git-fast-import

https://github.com/kilork/hg-git-fast-import

Another utility with the following features:

  1. Import of single and multiple Mercurial repositories to Git repository.
  2. Import of new revisions from previously imported Mercurial repositories to Git repository.
  3. Tags.
  4. Closed branches.
  5. Verification of the end result with diff.

You may download binary for you platform and put somewhere in path or install with cargo (require rust to be installed):

cargo install hg-git-fast-import 

Then usage is like this:

hg-git-fast-import single /path/to/source_hg /path/to/target_git 

It does not need Python and Mercurial to be installed. Advanced configuration allows to replace authors or branches, make branches prefixed and more.

1 Comment

a good tool, but it seems to be neglected these days and does not handle much of what you might find in the .hg/requires file in recent mercurial versions
1

If you're using github.com, they appear to have an import feature that lets you simply type in the URL of your hg project.

First create a new repository and then on the new repository's landing page scroll to the bottom and click the "Import code" button.

context for the import code button

Then type the URL of your previous repository and hit "Begin import".

context for "Begin import" button

Then GitHub takes care of the rest!

Github taking care of business for you

Note that GitHub will ask you for your credentials for the old repository if it needs them.

Ooh! I found the official guide

1 Comment

Unfortunately, Support for importing Mercurial, [...] repositories ended on April 12, 2024
-1

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.