Using Git subtrees to split a repository

https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/

We are in a position where we needed to create a new back-end后端 server for an application.

The current application is on a MEAN stack (Mongodb, Expressjs, Angularjs, Node.js), but a new client wants the backend to be deployed onto a JBoss server.  

This created a situation where we needed a completely different backend, but the front-end was shared between them.  

The approach we opted选择 for was using git subtrees to split the ui code into its own repository and shared between the nodejs repo and the Java repo.  

We did this by using the subtree features in git.

To be clear, I would only use this for very specific situations like this.  

If possible, keeping things simple in a single repository is usually best.  

But if you’re in the same situation, hopefully this will be helpful for you.

Splitting the Original Repository

The subtree commands effectively take a folder and split to another repository.  

Everything you want in the subtree repo will need to be in the same folder.

For the sake of this example, let’s assume you have a /lib folder that you want to extract to a separate repo.

假设你想把lib文件夹提取成一个独立的版本库

1.Create a new folder and initialize a bare git repo:

首先创建一个新的文件夹,并初始化一个空版本库

mkdir lib-repo
cd lib-repo
git init --bare

2.准备好一个远端版本库,和本地的进行映射

Create a remote repository in github or wherever for lib project and add that as the origin remote.

3.在原项目的文件夹中,执行subtree的命令,将文件夹处理到一个独立的分支split上

From within your parent project folder, use the subtree split command and put the lib folder in a separate branch:

https://github.com/apenwarr/git-subtree/blob/master/git-subtree.txt

prefix:

Specify the path in the repository to the subtree you want to manipulate.
This option is mandatory for all commands.

git subtree split --prefix=lib -b split

4.使用文件路径的方式,将split分支上推送到之第一步创建的版本库上

Push the contents to the of the split branch to your newly created bare repo using the file path to the repository.

git push ~/lib-repo split:master

This will push the split branch to your new repo as the master branch

From lib-repo push to your origin remote  

Now that lib folder lives in it’s new repository, you need to remove it from the parent repository and add the subtree back, from it’s new repository:

git remote add lib <url_to_lib_remote>
git rm -r lib
git add -A
git commit -am "removing lib folder"
git subtree add --prefix=lib lib master

还有更多的内容,有兴趣的可以去原文链接看

 https://www.cnblogs.com/chucklu/p/4647625.html   用git filter-branch拆分repository

原文地址:https://www.cnblogs.com/chucklu/p/5200204.html