Git Subtree
Table of Contents
Subtree fundamentals
With subtrees, there are no nested repos: there’s only one repo, the container, just like a regular codebase.
One of the key benefits of subtrees is to be able to mix container-specific customizations with general-purpose fixes and enhancements.
经由 Git Subtree 来维护的子项目代码,对于父项目来说是透明的,所有的开 发人员看到的就是一个普通的目录,原来怎么做现在依旧那么做,只需要维护这 个 Subtree 的人在合适的时候去做同步代码的操作。
Git Subtree1
假设,你要在各个项目里的components/zenjs这个目录对 http://github.com/youzan/zenjs.git 这个项目做Subtree
- 确保各个项目已经添加zenjs 这个 remote
git remote add zenjs http://github.com/youzan/zenjs.git
- 将zenjs添加到各个项目里
git subtree add --prefix=components/zenjs zenjs master
- 各项目更新zenjs代码的方法:
git subtree pull --prefix=components/zenjs zenjs master
- 各项目提交zenjs代码的方法:
git subtree push --prefix=components/zenjs zenjs hotfix/zenjs_xxxx
这会在远程的zenjs的仓库里生成一个叫
hotfix/zenjs_xxxx
的的分支,包含了你过去对components/zenjs 所有的更改记录 - 把
hotfix/zenjs_xxx
分支更新并合并到master并提交这样其他工程就可以更新到你提交的代码了。
Advance
重新split出一个新起点(这样,每次提交subtree的时候就不会从头遍历一遍了)
git subtree split --rejoin --prefix=components/zenjs --branch new_zenjs git push zenjs new_zenjs:master