Rio
Rio

Reputation: 1918

Extract part of a git repository?

Assume my git repository has the following structure:

/.git
/Project
/Project/SubProject-0
/Project/SubProject-1
/Project/SubProject-2

and the repository has quite some commits. Now one of the subprojects (SubProject-0) grows pretty big, and I want to take SubProject-0 out and set it up as a standalone project. Is it possible to extract all the commit history involving SubProject-0 from the parent git repository and move it to a new one?

Upvotes: 37

Views: 12610

Answers (2)

Tony K.
Tony K.

Reputation: 5605

I needed to do something similar, but I wanted to essentially move a subproject from one repo to another. What I did instead was to use fetch, since it can fetch objects from any source.

So, basically, I created a new branch, deleted the unneeded stuff in that branch, then used git fetch to pull the branch from one repo to another. Once I had the objects, merge did the trick.

E.g.

On the repository that has the original stuff:

    git checkout -b temp master
    git rm -r Unneeded_stuff
    git commit -m 'pruning'

Then you can fetch that branch from one repository into a completely different one (unrelated):

    cd /path/to/other/repository
    git fetch /path/to/source/repo temp:temp

where temp:temp means "fetch temp on the source and save it as temp in here". From there you can merge the result into your master.

    git merge temp

You can then delete the temp branches, since in the first case it isn't something you ever want to merge with the original repo, and in the second case you have merged it.

I'm sure these steps could be compressed a bit, but this set seems nice and clear.

Upvotes: 4

See http://git-scm.com/docs/git-filter-branch

I think you need something like

git filter-branch --subdirectory-filter Project/SubProject-0 --prune-empty -- --all

in a clone of the repository.

Upvotes: 40

Related Questions