Skip to content

Instantly share code, notes, and snippets.

@BjoernSchilberg
Last active May 21, 2023 19:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BjoernSchilberg/bae73857c120766cc3305217760b06b7 to your computer and use it in GitHub Desktop.
Save BjoernSchilberg/bae73857c120766cc3305217760b06b7 to your computer and use it in GitHub Desktop.
Migrate from mercurial to git with hg-git

Migrate from mercurial to git

Documentation & Source

Install latest development version

hg clone ssh://hg@bitbucket.org/durin42/hg-git

In .hgrc

hgext.bookmarks =
hggit = /tmp/hg-git/hggit

Downloading upstream dulwich source, and manually running make PYTHON=python2, followed by make PYTHON=python2 install DESTDIR="pwd", and finally reproducing ./usr/lib/python2.7/site-packages/dulwich* into the system’s python-2.7 runtime tree (in this case /usr/local/lib/python2.7/dist-packages/dulwich) with make install , gets hg-git working for me.

Migrate an existing Hg repository to local Git

Create empty git repo.

mkdir my-local-git
cd my-local-git
git init

Go to the mercurial repo.

cd mercurial-repo
hg bookmarks hg
hg push ../my-local-git

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 command (only necessary in the first run, later just use git merge or rebase).

cd my-local-git
git checkout -b master hg

Push an existing Hg repository to remote Git

cd mercurial-repo
hg bookmark -r default master # so a ref gets created

To avoid specifying the repo path when you push and pull, edit .hg/hgrc and add:

[paths]
default = git+ssh://git@github.com/schacon/some-repo.git

See the Mercurial docs for more detail on path settings.

hg push

This will convert all Mercurial data into Git objects and push them up to the Git server.

Tips

Migrate hg branches

In .hgrc

[git]
branch_bookmark_suffix=_bookmark

Create bookmarks from mercurial branches.

hg bookmark -r branch_name branch_name_boomark

In my-local-git repo

git push --all origin

Note: hg-git will strip off the _bookmark suffix from this bookmark name, and create a git branch without the _bookmark suffix.

@cmigliorini
Copy link

Great -- only I did not found hg-git on bitbucket : it's apparently moved to heptapod

@Spongman
Copy link

s/branch_name_boomark/branch_name_bookmark/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment