[教程][Git] 以Git Subtree 代替 Git Submodule
09 Nov 2013【何时用Submodule?】
比如说你有一个Git repo (repository ,下同)叫做web-project
然后你的web-project 需要用到另一个git repo 叫做 web-plugin
git 的 submodule 就能管理你的 子-repo ,也就是在 web-project 内clone
web-plugin ,然后你能管理、维护,就像操作普通git repo一样
可是git submodule 有点..
反正就是不太被大众欢迎,原因有很多.. (Google 下就有了)
然后就有了 git subtree
注:本文章需要先了解Git
【使用Git Subtree】
很简单
现在有一个local repo ,名叫 subtreetest
有一个文件叫做 readme.md
和一个commit history
还有另一个repo
叫做 testing
(我把它push 到github 上了 - https://github.com/garyng/testing)
【添加子repo】
现在我要在我的subtreetest 的 repo 内引用 testing 这个repo
运行
git subtree add –prefix=testing
https://github.com/garyng/testing.git master –squash
会看到这个:
其中的
–prefix 就是子目录的名称
–squash 代表将子repo的所有commit都挤成一个commit
现在subtreetest这个repo的目录已经变成了:
【pull下子repo的更新】
到testing 这个repo 做几个edits
然后在push
现在 testing 已经更新了
如果到subtreetest 跟新它的子repo (testing),像这样:
git subtree pull –prefix=testing
https://github.com/garyng/testing.git master –squash
然后commit history 就变成了
【用remote name 减少命令输入量】
有没有发现到需要输入的命令都很长?
现在将testing 的 pull/push URL 加入remote:
git remote add testing https://github.com/garyng/testing.git
加入subtree 的命令就成了:
git subtree add –prefix=testing testing master –squash
subtree pull 变成:
git subtree pull –prefix=testing testing master –squash
【在subtreetest下更新testing,再push上】
现在subtreetest 下更新 testing 目录下的文件
要push 回去的话:
git subtree push –prefix=testing testing master
好了!
Published by Gary Ng