Splitting Subsets of a Git Repository

Sometimes I need to split one or two useful files from a predominantly useless repository. This can be done with subtree merges and subdirectory filters, but if the files are not organised very well then this can prove difficult.

The following quick filter will get you just a few files, providing they don’t move around too much:

git filter-branch --index-filter \ 
  'git rm --cached -qr -- . && \ 
  git reset -q $GIT_COMMIT -- \ 
  FILENAME FILENAME2'

Note that I’m using index-filter rather than tree-filter.

We’re just deleting everything and then resetting the files we want all in one go.

As I wrote in my other post “merging a git repository”, we can move all those files to a subdirectory easily:

git filter-branch --prune-empty \
  --tree-filter 'mkdir -p new-subdir; \
  git ls-tree --name-only $GIT_COMMIT | \
  xargs -I FILES mv FILES new-subdir'

This means we can merge just a tiny part of a repository in very easily.

cd target-repo
git pull --allow-unrelated-histories filtered-repo

Modern versions of git want the --allow-unrelated-histories argument. You’ll need to rebase as well or those commits won’t have any of the stuff your existing repositories had which can prove pretty annoying.

Make sure it’s to your liking by checking out one of the revisions you just imported.

Advertisements
Splitting Subsets of a Git Repository

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s