Combine 2 Git Repos and Save History

I had a small problem. I needed to setup private repositories on GitHub for backing up my CubPath and ScoutTrail code and data. So, I signed up for the cheapest GitHub plan to get some private repos. The cheapest option provided 5 private repos, which is perfect for me right now. The problem is that I had 2 local repos for each app. This would use up 4 of my 5 remote repos. Not an ideal way of providing for future expansion.

My current app setup is this, I have 2 Xcode projects for each app. One project is the actual app, ScoutTrail. The other is a project, ScoutTrailData, I use for generating a SQLite database that is used to preload ScoutTrail Core Data store with all rank and merit badge requirements data.

The better design would be to include the ScoutTrailData project inside the ScoutTrail project. In addition to consolidating both projects into a single git repository, it will allow me to consolidate the Core Data model files.

But just copying ScoutTrailData as a subdirectory to ScoutTrail is not ideal. Why? I would lose the git history of ScoutTrailData. I am not a git expert. In fact, I am a novice. I can handle your basic init, add, commit, checkout, diff, pull and push. I’ve even been known to create a branch and try my hand with merge. But combining two local repos into one while maintaining history was beyond my skillset. So, I did what any self-respecting indie developer would do. I typed in “move git repo inside another one” and let the power of internet search supply me with knowledge. 2nd on the search results was a post on Greg Bayer’s (of Pulse) blog.

Create remote repos on GitHub

Create remote repos on GitHub for ScoutTrail and ScoutTrailData. Once I have everything moved and working correctly inside of ScoutTrail, I’ll go back and remove ScoutTrailData. After creating private repos, GitHub provides the commands for pushing an established local repo to a remote repo.

1
2
3
cd ScoutTrailData
git remote add origin <git repo ScoutTrailData url>
git push -u origin master

Prepare ScoutTrailData repo for the move

1
2
3
4
5
6
7
8
9
10
11
12
mkdir mergeDir
cd mergeDir
git clone <git repo ScoutTrailData url>
cd ScoutTrailData
# create subdir
mkdir ScoutTrailData
# mv * into it, needed to create ScoutTrailData subdirectory in ScoutTrail repo
mv * ScoutTrailData
git add .
# cleanup deletions caused by mv
for i in `git st | grep deleted | awk '{print $3}'`; do echo $i; git rm $i; done
git commit

Merge ScoutTrailData into ScoutTrail repo

1
2
3
4
5
6
7
cd mergeDir
git clone <git repo ScoutTrail url>
cd ScoutTrail
git remote scoutTrailData-branch ../ScoutTrailData
git pull scoutTrailData-branch master
git remote rm scoutTrailData-branch
git push

Now, ScoutTrailData is a subdirectory of ScoutTrail and both git histories have been saved.