The journey from HG to GIT
One of the most important constraints to migrate from a version control software to another is to keep all the branches and the development history intact. In many projects people choose the easy way by initiating a new project based on the last master branch. However, by doing so, they will end up losing all of that project’s history.
In this article I will explain how to use hg-git to convert a hg repository to git and keep all the history, branches, commits, etc.
First you should clone the hg-git repository
$ hg clone git://github.com/schacon/hg-git.git
To Enable the extension we should add the following section in ~/.hgrc file
[extensions] hggit = [path-to]/hg-git/hggit
If you try to clone a mercurial repository which contains a git repository you will get an error, so to avoid this you should add the following configuration to ~/.hgrc file
[git] blockdotgit = false
Each hg bookmark will be converted by hg-git to a git branch. So if you want to convert all the hg branches you should bookmark them all by using this script
#!/bin/bash -x for hgrevision in $(hg branches | grep -v 'inactive' | rev | cut -d' ' -f 1 | rev) do userevision=$(echo $hgrevision|cut -d ':' -f 2) hgbranchname=$(hg log -r $hgrevision | grep 'branch:' | cut -d ':' -f 2- | cut -b 7- | sed 's/[^a-zA-Z0-9]/ /g' | tr ' ' '_') hg bookmark -r $userevision "$hgbranchname" done
Once the hg-git is configured you should initiate a new git repository using this command
And in the hg repository folder we should execute this command to run the conversion
hg push /path-to-git-rep
Finally if your hg repository contains a git repository we should clean the new converted one to delete all the .git folders from the history using the bfg repo-cleaner. So you should download the tool from this url https://rtyley.github.io/bfg-repo-cleaner/ and run the following command
$ java -jar bfg.jar --delete-folders .git --delete-files .git --no-blob-protection my-repo.git
After this you can push the new repository to the remote server and check that all the branches have been converted.