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

Abhinav
1 min readOct 18, 2015

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 move_repo.sh.

Place this file one level above CameraSpike and Spikes.

Then run sh ./move_repo.sh CameraSpike Spikes

Reference http://www.nomachetejuggling.com/2011/09/12/moving-one-git-repo-into-another-as-subdirectory/ http://superuser.com/a/165534/127944 http://stackoverflow.com/questions/18667308/git-move-file-and-directory-into-a-sub-directory-along-with-commit-history

We make tech easy to learn with our interactive online courses at https://interleap.co .

Originally published at http://aspoonfulofgyan.wordpress.com on October 18, 2015.

--

--