Tuesday, January 10, 2012

Git: New Repository From A Subdirectory

Suppose I have this repository layout to start with
XYZ/
    .git/
    XY1/
    ABC/
    XY2/
And I want a new repository for ABS only, here is the excellent answer from over at stack overflow.

You want to clone your repository and then use git filter-branch to mark everything but the subdirectory you want in your new repo to be garbage-collected. To clone your local repository:
 $ git clone --no-hardlinks /XYZ /ABC
The --no-hardlinks switch makes git use real file copies instead of hardlinking when cloning a local repository. The garbage collection and pruning actions will only work on blobs (file contents), not links.
Then just filter-branch and reset to exclude the other files, so they can be pruned:
 $ git filter-branch --subdirectory-filter ABC HEAD -- --all
Then delete the backup reflogs so the space can be truly reclaimed (although now the operation is destructive). The -- --all keeps the project branches and tags included in the the new repo.
 $ git reset --hard
 $ rm -rf .git/refs/original/
 $ git reflog expire --expire=now --all
 $ git gc --aggressive --prune=now
and now you have a local git repository of the ABC sub-directory with all its history preserved.

No comments:

Post a Comment