Published on

Moving folder and history between git repos

Authors

Problem : One part of a git-tracked repo has become an independent project in its own right. Moreover, there's no need for participants in that project to have access to the rest of the repo.

Idea : Move the independent project out into a separate git repo, preserving all the commit history.

Huge props to the following :

Initially : Create a fresh clone of the source repo (this will be removed later : don't do this on a working copy, just in case everything goes awry):

git clone git@git.EXAMPLE.com:sketchpad.git
cd sketchpad/

Now, let's filter everything in the directory 'android-edutiger' out of this local copy - all the changes from here are what will be moved across (disconnect from the main repo) :

git filter-branch --subdirectory-filter android-edutiger -- -- all
git remote rm origin

Now, tidying up the 'source' of the code prior to pulling it into the other repo, create a new directory, and move all the relevant files into it :

mkdir android-native
git mv AndroidManifest.xml android-native/
git mv Notes.txt android-native/
git mv pro* android-native/
git mv res android-native/
git mv src android-native/
git commit -a -m "Moved into directory prior to moving repo"

Using a (potentially) new repo (potentially on GitHub) and cloning it down to the local directory (let's call it separated-out).

Now go into the repo separated-out and pull the stuff over from the local remote (which we'll temporarily name SKETCHPAD) :

cd ..
cd separated-out/
git remote add SKETCHPAD ../sketchpad/
git fetch SKETCHPAD
git branch SKETCHPAD remotes/SKETCHPAD/master
git merge SKETCHPAD

Now the new files should have arrived - disconnect from the local remote "SKETCHPAD", and clean up:

ls -l android-native/
git remote rm SKETCHPAD
git branch -d  SKETCHPAD
git push origin master
git push
git pull

Get rid of the hacked around 'sketchpad' (and a real working version will now need to have the same stuff cleaned out too, first-off by issuing a git pull) :

cd ../
rm -rf sketchpad