关于子仓库或者说是仓库共用,git官方推荐的工具是git subtree。 我本身也用了一段时间的git subtree,感受比git submodule好用,可是也有一些缺点,在可接受的范围内。
因此对于仓库共用,在git subtree 与 git submodule之中选择的话,我推荐git subtree。html
git subtree 能够实现一个仓库做为其余仓库的子仓库。git
使用git subtree 有如下几个缘由:github
旧版本的git也支持(最老版本能够到 v1.5.2).工具
git subtree与git submodule不一样,它不增长任何像.gitmodule
这样的新的元数据文件.post
git subtree对于项目中的其余成员透明,意味着能够不知道git subtree的存在.学习
固然,git subtree也有它的缺点,可是这些缺点还在能够接受的范围内:spa
必须学习新的指令(如:git subtree).code
子仓库的更新与推送指令相对复杂。htm
git subtree的主要命令有:blog
git subtree add --prefix=<prefix> <commit> git subtree add --prefix=<prefix> <repository> <ref> git subtree pull --prefix=<prefix> <repository> <ref> git subtree push --prefix=<prefix> <repository> <ref> git subtree merge --prefix=<prefix> <commit> git subtree split --prefix=<prefix> [OPTIONS] [<commit>]
咱们先准备一个仓库叫photoshop,一个仓库叫libpng,而后咱们但愿把libpng做为photoshop的子仓库。
photoshop的路径为https://github.com/test/photoshop.git
,仓库里的文件有:
photoshop | |-- photoshop.c |-- photoshop.h |-- main.c \-- README.md
libPNG的路径为https://github.com/test/libpng.git
,仓库里的文件有:
libpng | |-- libpng.c |-- libpng.h \-- README.md
如下操做均位于父仓库的根目录中。
咱们执行如下命令把libpng添加到photoshop中:
git subtree add --prefix=sub/libpng https://github.com/test/libpng.git master --squash
(--squash
参数表示不拉取历史信息,而只生成一条commit信息。)
执行git status
能够看到提示新增两条commit:
git log
查看详细修改:
执行git push
把修改推送到远端photoshop仓库,如今本地仓库与远端仓库的目录结构为:
photoshop | |-- sub/ | | | \--libpng/ | | | |-- libpng.c | |-- libpng.h | \-- README.md | |-- photoshop.c |-- photoshop.h |-- main.c \-- README.md
注意,如今的photoshop仓库对于其余项目人员来讲,能够不须要知道libpng是一个子仓库。什么意思呢?
当你git clone
或者git pull
的时候,你拉取到的是整个photoshop(包括libpng在内,libpng就至关于photoshop里的一个普通目录);当你修改了libpng里的内容后执行git push
,你将会把修改push到photoshop上。
也就是说photoshop仓库下的libpng与其余文件无异。
若是源libpng仓库更新了,photoshop里的libpng如何拉取更新?使用git subtree pull
,例如:
git subtree pull --prefix=sub/libpng https://github.com/test/libpng.git master --squash
若是在photoshop仓库里修改了libpng,而后想把这个修改推送到源libpng仓库呢?使用git subtree push
,例如:
git subtree push --prefix=sub/libpng https://github.com/test/libpng.git master
咱们已经知道了git subtree 的命令的基本用法,可是上述几个命令仍是显得有点复杂,特别是子仓库的源仓库地址,特别不方便记忆。
这里咱们把子仓库的地址做为一个remote,方便记忆:
git remote add -f libpng https://github.com/test/libpng.git
而后能够这样来使用git subtree命令:
git subtree add --prefix=sub/libpng libpng master --squash git subtree pull --prefix=sub/libpng libpng master --squash git subtree push --prefix=sub/libpng libpng master