【何时用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 就是子目录的名称现在subtreetest这个repo的目录已经变成了:
--squash 代表将子repo的所有commit都挤成一个commit
testing 目录下:
【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
回到testing 下
git pull
就能看到更新了!
好了!
今晚睡觉前要搞懂啊!!!
回复删除