Merging a git repo into a larger project

This page shows how to merge multiple git repos under a single repo. I start a project that I am unsure will go anywhere, with a name that is specific to the project. I will call this project ProjA.

I now want to combine this project with a larger project, while still keeping the repo history intact. The larger repo will be called MainProj.

Modifying ProjA's directory stucture history

ProjA will need to be modified in order to combine it correctly with the other project. It will reside in a new directory called newProjA in the main projects repo at the end of the operation, so lets move it there now. This is done with git filter-branch and the step is dangerous, so make a backup first.

cp -a ~/ProjA ~/ProjA-bak

Now onto the renaming of the repos history.

git filter-branch --prune-empty --tree-filter '
if [[ ! -e  ]]; then
    mkdir -p newProjA
    git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files newProjA
fi' 

Fetching the new repo and merging

git remote add ProjAnew ~/ProjA
git fetch ProjAnew
git merge ProjAnew/master