我也同意Ronald William's answer。 Git 子模块的主要目的是更新从外部世界获取的代码,如果该代码被更新修改,则无需提交更改。
Composer 包管理系统也是如此。实际上 they don't recommend to commit those changes either 并忽略项目根目录中 .gitignore 中的 vendor 文件夹。
如果您尝试提交此文件夹,那将是一场噩梦,因为某些 vendor/some_repo 可能是开发版本,因此它们有一个 .git 文件夹,这会导致所有这些即使您不使用 git submodule add 添加软件包,它们也会成为子模块。如果您在嵌套的 .git 存储库中修改 some_file,您可能会看到类似的内容:
~/project_root $ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
# (commit or discard the untracked or modified content in submodules)
#
# modified: vendor/nested_repo (modified content)
请注意 modified content in submodules 条目,并且您在输出中看不到 some_file 名称。相反,您会看到 (modified content) 通知,因为 root_project .git 将 vendor/nested_repo 视为子模块,并且不会跟踪该文件夹中的单个文件。
如果您运行 git add --all,则在您在 vendor/nested_repo 中提交更改之前,您将不会得到任何结果,只有在此之后,您才能在根存储库中提交更改。
不要这样做。相反,如果您想将项目保留为一个完整的 .git 存储库(任何,不仅是 Composer 构建的存储库),这有时非常方便,请将此条目添加到根 .gitignore BEFORE 初始提交:
.git
!/.git
不幸的是,要使整个配方起作用,您需要为以后要单独修改的每个嵌套存储库运行git add 命令。请注意,存储库路径中的尾部斜杠是必须。
~/project_root $ git add vendor/some_repo/ vendor/another_repo/
然后修改vendor/some_repo中的some_file,看看有什么区别:
~/project_root $ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: vendor/some_repo/some_file
这样您就可以像往常一样在project_root 中运行git add --all 然后git commit "Changes ..."。