Tuesday, October 7, 2014

Adventures in Git OR How to fork folders in one Repo to their own repo with history

In short, I had one repo, with multiple folders where each really represented its own project and I needed to fork each folder to a separate repo WITH history. This is the solution I came up with. This does require git 1.8.4 (git subtree split).

git subtree split --prefix=sourceFolderInAnyExistingProject -b anyNewBranchNameForFork

mkdir ../someNewRepoFolder

cd ../someNewRepoFolder

git init --bare

cd backToOriginalWorkingFolder

git push ../someNewRepoFolder anyNewBranchNameForFork:master

cd ../someNewRepoFolder

git remote add origin YourNewRepoRemote

git push -u origin --all

cd ..

rm -rf somenewRepoFolder

git clone YournewRepoRemote someNewRepoFolder

Sorry if that's not clear enough, but basically, you use git subtree split to take ANY folder presently in a repo and create a branch that's filtered to have just that folder and all the history for it. Then you create a bare repo, push the branch into it, then push that repo (without working folder) to your new repo, then either trash the folder or clone the repo into another one and BAM, you've forked not only an existing repo, but just one folder in it if you prefer (I had many "projects" in one repo that needed to be forked individually so this was useful for me) and history stays intact.