title: git: How to split off library from project? filter-branch, subtree?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>git-subtree seems like a solution here, but it does not completely fit.</p>
  
  <p>My directory layout is something like this (since it is a Java project):</p>
  
  <ul>
  <li>fencing-game  <em>(git workdir)</em>
  <ul>
  <li>src
  <ul>
  <li>de
  <ul>
  <li>fencing_game
  <ul>
  <li><strong>transport</strong>   <em>(my library)</em>
  <ul>
  <li><strong>protocol</strong>  <em>(part of the library)</em></li>
  <li>fencing <em>(part of the main project interfacing with the library)</em></li>
  <li>client <em>(part of the main project interfacing with the library)</em></li>
  <li>server <em>(part of the main project interfacing with the library)</em></li>
  </ul></li>
  <li>client <em>(part of the main project)</em></li>
  <li>server <em>(part of the main project)</em></li>
  <li>...  <em>(part of the main project)</em></li>
  </ul></li>
  </ul></li>
  </ul></li>
  <li><em>other files and directories (build system, website and such - part of the main project)</em></li>
  </ul></li>
  </ul>
  
  <p>After the split, I want the library's directory layout look like this (including any files directly in the bold directories):</p>
  
  <ul>
  <li>my-library <em>(name to be determined)</em>
  <ul>
  <li>src
  <ul>
  <li>de
  <ul>
  <li>fencing_game
  <ul>
  <li><strong>transport</strong>   <em>(my library)</em>
  <ul>
  <li><strong>protocol</strong>  <em>(part of the library)</em></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p><strong>The history should also contain just the part of the main project's history which touches this part of the repository.</strong></p>
  
  <p>A first look showed me <code>git-subtree split --prefix=src/de/fencing_ame/transport</code>, but this will</p>
  
  <ol>
  <li>give me a tree rooted in <code>transport</code> (which will not compile) and</li>
  <li>include the <code>transport/client</code>, <code>transport/server</code> and <code>transport/fencing</code> directories.</li>
  </ol>
  
  <p>The first point could be mitigated by using <code>git subtree add --prefix=src/de/fencing_ame/transport &lt;commit&gt;</code> 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).</p>
  
  <p>Do I have to use <code>git filter-branch</code> here?</p>
  
  <p>After the split, I want to be able to <em>import back</em> 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 </p>
  
  <p><ul>
  <li>fencing-game  <em>(git workdir)</em>
  <ul>
  <li>src
  <ul>
  <li>de
  <ul>
  <li>fencing_game
  <ul>
  <li>transport   <em>(empty)</em>
  <ul>
  <li>fencing <em>(part of the main project interfacing with the library)</em></li>
  <li>client <em>(part of the main project interfacing with the library)</em></li>
  <li>server <em>(part of the main project interfacing with the library)</em></li>
  </ul></li>
  <li>client <em>(part of the main project)</em></li>
  <li>server <em>(part of the main project)</em></li>
  <li>...  <em>(part of the main project)</em></li>
  </ul></li>
  </ul></li>
  </ul></li>
  <li>my-library
  <ul>
  <li>src
  <ul>
  <li>de
  <ul>
  <li>fencing_game
  <ul>
  <li><strong>transport</strong>   <em>(my library)</em>
  <ul>
  <li><strong>protocol</strong>  <em>(part of the library)</em></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  <li><em>other files and directories (build system, website and such - part of the main project)</em></li>
  </ul></li>
  </ul>
  <strong>What would be the most pain-free way to do this?</strong> Are there other tools than git-subtree and git-filter-branch for this goal?</p>