Migrate from Mercurial Hg to Git

I had one last Mercurial project that I still actively developed. Everything new I’ve completed restarted with Git but this was a last hold out.

I blindly followed this Stack Overflow answer which says to use fast-export. However I ran into the following problem, and the Stack Overflow answer didn’t help much.

$ git push origin master
remote: error: refusing to update checked out branch: refs/heads/master
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/home/mirandam/git/project'

After some trial and error, the following steps worked exactly how I expected.

  • My Git projects are in ~/git (where the converted project will end up)
  • My Mercurial projects are in ~/hg (this is where the project to be converted will live)
  • I use ~/work as my working directory (where I clone, checkout, etc.)
  • I use ~/tmp as just a scratch temporary directory

[mirandam@atlas ~]$ mkdir ~/git/project-new
[mirandam@atlas ~]$ cd ~/git/project-new
[mirandam@atlas project-new]$ git init --bare
[mirandam@atlas project-new]$ cd ~/tmp
[mirandam@atlas tmp]$ git clone git://repo.or.cz/fast-export.git
[mirandam@atlas tmp]$ cd ~/work/
[mirandam@atlas work]$ git clone ~/git/project-new
[mirandam@atlas work]$ cd ~/work/project-new
[mirandam@atlas project-new]$ ~/tmp/fast-export/hg-fast-export.sh -r ~/hg/project-old
[mirandam@atlas project-new]$ git checkout HEAD
[mirandam@atlas project-new]$ git push origin master

Now in my work directory I can perform changes and continue working.

Leave a Reply

Your email address will not be published. Required fields are marked *