Git 子模块被忽略

Git submodule is ignored

当我尝试通过

添加子模块时
git submodule add git@domian:repo.git contact

我收到以下消息:

The following path is ignored by one of your .gitignore files:

contact

Use -f if you really want to add it.

这是我的 .gitignore:

# Ignore everything
*

# But not these files:
!*.py
!*.md
!*.gitignore
!.gitmodules
!contact/

使用建议的 -f 选项已解决,但我很好奇为什么 .gitignore 中的 !contact/ 条目没有缓解问题。

在你的特定情况下我没有遇到那个错误(我有 git 版本 2.21.0.windows.1)。

我在尝试在父存储库之外添加子模块时确实遇到了这个错误(这显然不受支持):

$ git submodule add https://github.com/user/repo ../repo
The following path is ignored by one of your .gitignore files:
../repo
Use -f if you really want to add it.

最好的猜测是这是一个错误...所以将 !contact/ 添加到您的 .gitignore 并不能解决它,因为它实际上不是导致问题的 .gitignore

你有什么 git 版本?您可以下载source code for your particular version, search for the error message (e.g. here it is v2.21), 并跟踪代码找出问题所在。

一个子模块由一个gitlink (a special entry 160000 in the Git repository index)和一个远程的URL+.gitmodules中的路径组成。

所以排除 !contact/ 仍然会忽略 gitlink“contact”(它不是文件夹 contact/,而是一个特殊的“file")

这样会更好,并允许 git submodule add 继续:

!contact

并且如果任何其他原因仍然会阻止 git submodule addnext Git 2.26 (Q1 2020) will provide a more helpful error message