Moving all Contents of a Git Repository into another as a Sub-Directory

This post teaches you how to move the entire contents of a git repository into another as a sub-directory, and maintain the git history of both.

Here’s my scenario — I have two git repositories — CameraSpike and Spikes. I want to move CameraSpike into Spikes as a sub-directory, and preserve the commit history. Here’s the script I ended up writing for it. It has references and copy pastes from various channels on the internet, and some of my own work. It’s hacky, but works. For now, I ignore all merge conflicts by using the files from the destination. In my case, the file concerned was .gitignore.

The Script -

cd $1
mkdir .$1
git mv * .$1
git mv .$1 $1
git add .
git commit -a -m “Preparing $1 for move”
cd ../$2
git remote add temp ../$1
git fetch temp
git merge temp/master
git checkout --ours .
git add .
git commit
git remote rm temp

Directions -

Copy the contents of the script into

Place this file one level above CameraSpike and Spikes.

Then run sh ./ CameraSpike Spikes


We make tech easy to learn with our interactive online courses at .

Originally published at on October 18, 2015.

Educator, Founder @ Interleap

Love podcasts or audiobooks? Learn on the go with our new app.