Migrate Git directory to it's own repository



So say we have an existing Git project "CoolProject" like this
  • projects/coolproject/
  • projects/coolproject/subproject/

So it was all and well, but now we think it would make much more sense for subproject to be it's own project with it's own git repo.

But we'd like to move that without loosing all the history and stuff.

As usual Git can do it, as usual it's not all that easy to figure out:

First we create a temporary clone of the project in /tmp

git clone --no-hardlinks git@bitbucket.org:myusername/coolproject.git /tmp/coolproject


Then we make SubProject the root, removing the rest, by using git filter-branch

cd /tmp/coolproject
git filter-branch --subdirectory-filter subproject  --prune-empty --tag-name-filter cat -- --all


Then we create a new clone of that for a clean-trimmed down project:

 git clone file:///tmp/coolproject/ /tmp/subproject


Now do some cleanup to remove all history stuff we no longer care about and reduce the repo size:

cd /tmp/subproject
 git reflog expire --expire=now --all
git repack -ad
git gc --prune=now


Now maybe we have created an empty repo for subproject on bitbucket or somewhere else, so we can do the "initial" push/mirror there:
git push --mirror git@bitbucket.org:myusername/subproject.git


Finally, now that it's in a clean state, we can pull the clean project from the new repo:

cd ~/projects/
git clone git@bitbucket.org:myusername/subproject.git


Now we have a nice new clean subproject project in projects/subproject , only thing left would be to remove SubProject from CoolProject and push that if you want to

cd ~/projects/coolproject/
git rm -r subproject
git commit -m "removal of subproject"
git push
rm -rf subproject



Comments

Add a new Comment