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

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(406)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2014-02-19 FieldOffset
2014-02-19 关于指针的解释
点击右上角即可分享
微信分享提示