So, I've a bigger (closed source) project, and in the context of this project created a library which could also be useful elsewhere, I think.
I now want to split off the library in its own project, which could go as open source on github or similar. Of course, the library (and its history there) should contain no traces of our project.
git-subtree seems like a solution here, but it does not completely fit.
My directory layout is something like this (since it is a Java project):
After the split, I want the library's directory layout look like this (including any files directly in the bold directories):
The history should also contain just the part of the main project's history which touches this part of the repository.
A first look showed me git-subtree split --prefix=src/de/fencing_ame/transport
, but this will
transport
(which will not compile) andtransport/client
, transport/server
and transport/fencing
directories.The first point could be mitigated by using git subtree add --prefix=src/de/fencing_ame/transport <commit>
on the receiving side, but I don't think git-subtree can do much against exporting also these subdirectories. (The idea really is to just be able to share the complete tree here).
Do I have to use git filter-branch
here?
After the split, I want to be able to import back the library in my main project, either using git-subtree or git-submodule, in a separate subdirectory rather than where it is now. I imagine the layout this way
This takes advantage of the fact you want to move to another repo, so we can extract the subtree, and then relocate it in separate steps. Use git subtree split to extract the files you want to the an intermediate branch in your repository (you have already done this). git subtree split -P lib3 -b new-branch.
Git addresses this issue using submodules. Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.
git subtree lets you nest one repository inside another as a sub-directory. It is one of several ways Git projects can manage project dependencies. Why you may want to consider git subtree. Management of a simple workflow is easy.
I think you've got some real spelunking to do. If you just want to split off "protocol", you can do that with "git subtree split ..." or "git filter-branch ..."
git filter-branch --subdirectory-filter fencing-game/src/de/fencing_game/transport/protocol -- --all
But if you have files in transport as well as transport/protocol, it starts to get hairy.
I wrote some custom tools to do this for a project I was on. They're not published anywhere, but you can do something similar with reposurgeon.
This seems to be a common request, however I don't think there's a simple answer, when the folders are mixed together like that.
The general method I suggest to split out the library mixed in with other folders is this:
Make a branch with the new root for the library:
git subtree split -P src/de/fencing_game -b temp-br git checkout temp-br # -or-, if you really want to keep the full path: git checkout -b temp-br cd src/de/fencing_game
Then use something to re-write history to remove the parts that aren't part of the library. I'm not expert on this but I was able to experiment and found something like this to work:
git filter-branch --tag-name-filter cat --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch client server otherstuff' HEAD # also clear out stuff from the sub dir cd transport git filter-branch --tag-name-filter cat --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch fencing client server' HEAD
Note: You might need to delete the back-up made by filter-branch between successive commands.
git update-ref -d refs/original/refs/heads/temp-br
Lastly, just create a new repo for the library and pull in everything that's left:
cd <new-lib-repo> git init git pull <original-repo> temp-br
I recommend that your final library path be more like /transport/protocol
instead of the full parent project path since that seems kind of tied to the project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With