I recently had to merge a number of independent projects into a larger project to enable a long-term project reorganization effort. Since we’re using Git for both projects, this was quite easy, but it required some investigating to figure out how to do it.
So you have two projects, old-project and new-project. You want to end up with a subdirectory inside of new-project called old-project with all of the code from old-project, and you want to preserve that project’s history.
For the purposes of this demonstration, old-project is cloned at /home/machete/old-project and new-project is cloned at /home/machete/new-project.
At the end of this, we want there to be a /home/machete/new-project/stuff/old-project with all of the commits from old-project intact.
The way we’re going to do this, it’s going to completely overlay old-project on top of new-project, so first we need to adjust the directories inside of old-project
cd /home/machete/old-project mkdir -p ../stuff/old-project mv * .gitignore ../stuff/old-project mv ../stuff . git commit -a -m "Preparing old project for move"
This just moves all of the files in old-project into a subdirectory structure matching what we want to have inside of new-project, then makes a commit.
Next we need to copy the history of this repository into new-project. So we don’t have to push our temporary move anywhere, we’re going to add our local directory as a remote git repository, then pull its changes in.
cd /home/machete/new-project git remote add temp file:///home/machete/old-project git pull temp master git remote rm temp
This pulls the entirety of old-project’s history in. Since we did a pull (a fetch and then a merge), we will have a new commit at HEAD that has two parents, one for the regular old-project codeline, and one for the old new-project codeline.
That’s it, you can push new-project anywhere and the next fetch will pull down all of old-project‘s history.
Absolutely No Machete Juggling is a blog about software, programming, computers, and me. I'm a programmer working in Colorado, mostly with Java and Ruby. 
