对你的问题的简短回答是:没办法。信息写在提交的超级项目中,存储有关子模块更改的信息。每次跑
git clone --recursive superproject-url
或者
git checkout --recursive some-old-commit
Git 在 HEAD 中查找子模块的提交或正在检出的旧提交并检出存储的子模块提交。
如果你想更改存储在 HEAD 中的提交:转到本地克隆的子模块,检查提交,返回到超级项目,添加并提交子模块中的更改,推送:
cd subdir
git checkout branch-tag-or-ID
cd .. # back to the superproject
git add subdir
git commit -m "Change in subdir" subdir
如果要更改为的子模块中的提交是 HEAD,则可以在超级项目中执行
git submodule update --remote subdir
git commit -m "Change in subdir" subdir
除了命令行选项,您还可以执行(使用 shell 脚本或 git 别名)git clone --recursive && cd subdir && git checkout commit-ID。这是唯一的方法。就像是
# .gitconfig
[alias]
clone-sub = "!f() { git clone --recursive "$1" && cd "$2" && git checkout "$3"; }; f"
用法:git clone-sub superproject-url submodule-name commit-ID