Apr 25, 2017

Converting Mercurial (hg) repositories to GIT

Git has an extension command remote-hg which allows to directly interact with an HG (mercurial) repository.

To keep the repository as a mercurial repo you can directly checkout with git and work as if the
repo would be a git repo:

git clone hg::http//hg.server.home/repo

To do the conversion to git completely, I started a container with Fedora as the base image:

docker run -it --name fed --hostname=fed fedora bash

Install git and the remote-hg extension in the container

dnf install git-remote-hg.noarch git.x86_64

Clone the mercurial repo:

git clone hg::http://hg.server.home/repo


To get the branches, checkout them locally:

for remote in `git branch -r |grep branches | sed -e 's#origin/##'`; do
git checkout --track -b $remote
done

Now rename the branches  if you like

for b in `git branch -l | grep branches | sed -e 's#branches/##'`; do
git branch -m branches/$b $b;
done

Add the new GIT repo as a remote and push the branches and the tags:

git remote add <name> http://git.server.home/scm/git/<targetRepoInGit>
git push --all <name>
git push --tags <name> 


Now the Repo is on the GIT-Server http://git.server.home/.