git: How to split off library from project? filter-branch, subtree?

前端 未结 5 1569
無奈伤痛
無奈伤痛 2020-12-24 02:34

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 o

5条回答
  •  攒了一身酷
    2020-12-24 02:58

    Splitting a subtree mixed with files from the parent project

    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:

    1. 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
      
    2. 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
      
    3. Lastly, just create a new repo for the library and pull in everything that's left:

      cd 
      git init
      git pull  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.

提交回复
热议问题