You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git子模块push操作解析:提交的内容究竟是什么?

关于Git子模块提交内容的疑问

我通过方案本地拉取子模块subby的最新提交:

$ git clone --recurse-submodules <omitted>/importer.git
$ cd importer
$ cat subby/README.md
cat: subby/README.md: No such file or directory
#
# good ! it's not supposed to exist
#
$ git submodule foreach git pull origin master
Entering 'subby'
<omitted>
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ cat subby/README.md 
Hello oren
#
# good ! now it exists
#

但现在我想要推送该修改:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   subby (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

我尝试执行以下命令成功完成推送:

$ git add subby
$ git commit -m "updating subby"
$ git push

但我到底提交了什么?是子文件夹吗?


解答

你提交的不是子文件夹里的实际文件内容,而是Git子模块的提交哈希值。

Git子模块的本质是在父仓库中存储一条特殊记录,这条记录里保存着子模块仓库当前指向的具体提交ID。当你在子模块里拉取最新提交后,子模块的HEAD指向了新的哈希值,父仓库就会检测到这条记录发生了变化。

你执行git add subby并提交时,实际是把父仓库中记录的subby子模块的最新提交ID更新并保存下来。其他克隆这个父仓库的人,拉取你推送的提交后,需要执行git submodule update,才能让子模块跳转到你指定的那个最新提交上。

简单说:父仓库只存子模块的「版本指针」,不存子模块的文件内容;你提交的就是这个指针的更新,告诉父仓库现在该用子模块的哪个版本。


内容的提问来源于stack exchange,提问作者OrenIshShalom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:20:28