Subtree 与 Submodule

subtree 和 submodule 的目的都是用于 git 子仓库管理,二者的主要区别在于,subtree 属于拷贝子仓库,而 submodule 属于引用子仓库。

1、Subtree vs Submodule

2、Subtree 命令行简化

subtree 在操作时,命令行较长,可以使用 remote 配置简化,例如

# 以下为标准 subtree add 命令行示例
git subtree add --prefix=centos-config --squash git@github.com:kaiye/centos-config.git master

# 可以简化为
# 1. 先为远程子仓库配置一个别名,便于后续的 pull 与 push 操作,这里例子以 centos 为别名
git remote add centos git@github.com:kaiye/centos-config.git # gra centos ...

# 2. 其中 --prefix= 简写为 -P,配置 --squash 表示不拉取子仓库的历史提交记录
git subtree add -P centos-config --squash centos master

# 后续更新子仓库可以使用
git subtree pull -P centos-config centos master

# 若发生 fatal: refusing to merge unrelated histories 报错,加上 --squash 参数即可

3、git submodule update 出错解决方案

假如在执行 git submodule update 时出现以下类似错误信息:

fatal: reference is not a tree: f869da471c5d8a185cd110bbe4842d6757b002f5
Unable to checkout 'f869da471c5d8a185cd110bbe4842d6757b002f5' in submodule path 'centos-config'

发生错误的原因是,centos-config 子仓库在某电脑 A 的「本地」commit 了新的版本 「f869da471c5d8a185cd110bbe4842d6757b002f5」,且该次 commit 未 push origin。但其父级仓库中引用了该子仓库的版本号,且将引用记录 push origin,导致其他用户无法 update 。

解决方案是,在电脑 A 上将子仓库 push origin 后,在其他客户机上执行 git submodule update 。或者使用 git reset,将子仓库的引用版本号还原成 origin 上存在的最新版本号。

Last updated