Moving a git repo to a subdir of an existing repo, keeping history

Via http://www.nomachetejuggling.com/2011/09/12/moving-one-git-repo-into-another-as-subdirectory/.

I wanted to move several git repositories into an existing repo as part of a restructuring of related projects. A quick google turned up the above link and  I reproduced the important parts here for safekeeping, adjusted slightly for Windows:

  1. Get a local copy of the repository containing the project being pulled in
  2. Modify the local copy to move all of the files into a subdirectory
  3. Add the local copy as a fake “remote” to our larger project
  4. Pull the local “remote” in, thereby pulling the entire history along with it
cd old-project
md new-name
move *.* .\new-name 
git commit -a -m "Preparing old project for move"

After that, the old repo contains only 1 folder, new-name, which will become the name of the subfolder of  this project in the target repository.

cd new-project 
git remote add temp https://url-to-old/repository.git
git fetch temp 
git merge temp/master --allow-unrelated-histories
git remote rm temp

Now, push the new-project (or existing project) and you’re done, keeping the history of the old-project in it’s new home.

Advertisements