Merging a git and mercurial project into one git repository

For this project the back-end was written in Java and stored in a mercurial (hg) repository, and the front-end was written in Ruby and stored in a git repository. To allow having branches across the project, we wanted to merge the two repositories into one. The target SCM was git.

In the result repository, there needed two be two subdirectories, back-end and front-end which contain the original projects.

Let’s start by converting the mercurial repository to a git:

git init back-end
cd back-end
hg-fast-export -r /path/to_hg/repo
git checkout 

Now move the contents into a subdirectory, rewriting history to preserve it:

git filter-branch --index-filter \
    'git ls-files -s | sed "s-\t\"*-&back-end/-" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --index-info &&
    mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

This repository is now ready for merging.

cd ..

Now get the front-end repo and put its content in a subdirectory preserving history:

git clone <front-end-git-url> front-end
cd front-end
it filter-branch --index-filter \
    'git ls-files -s | sed "s-\t\"*-&front-end/-" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
            git update-index --index-info &&
    mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD
cd ..

Now create a new repository for the combined project:

git init merged
cd merged
git pull ../back-end
git pull ../front-end

Done. Add the remote origin and push to finish.

Leave a Reply

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

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*