Git 子模块显示新提交,子模块状态表示没有提交

Cat*_*way 76 git

在 git 存储库中,我设置了 .gitmodules 文件以引用 github 存储库:

[submodule "src/repo"]
    path = src/repo
    url = repourl
Run Code Online (Sandbox Code Playgroud)

当我在这个 repo 上“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 checkout -- <file>..." to discard changes in working directory)

modified:   src/repo (new commits)
Run Code Online (Sandbox Code Playgroud)

如果我 cd 进入 src/repo 和 repo 上的 git status,它说没有什么可提交的。

为什么我的顶级 git repo 抱怨?

yae*_*shi 53

这是因为应该为每个子模块检出 Git 记录哪些提交(不是分支或标签,仅用 SHA-1 哈希表示的一个提交)。如果您更改子模块目录中的某些内容,Git 将检测到它并敦促您在顶级存储库中提交这些更改。

git diff在顶级存储库中运行以显示实际改变了 Git 认为的内容。如果您已经在子模块中进行了一些提交(因此子模块中的“干净”),它会报告子模块的哈希更改。

$ git diff
diff --git a/src/repo b/src/repo
index b0c86e2..a893d84 160000
--- a/src/repo
+++ b/src/repo
@@ -1 +1 @@
-Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea
+Subproject commit a893d84d323cf411eadf19569d90779610b10280
Run Code Online (Sandbox Code Playgroud)

否则,它会显示-dirty无法在顶级存储库中暂存或提交的哈希更改。 git status还声称子模块具有未跟踪/修改的内容。

$ git diff
diff --git a/src/repo b/src/repo
--- a/src/repo
+++ b/src/repo
@@ -1 +1 @@
-Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea
+Subproject commit b0c86e28675c9591df51eedc928f991ca42f5fea-dirty

$ 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:   src/repo (untracked content)

no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

要更新子模块应检出哪些提交记录,除了提交子模块中的更改外,您还需要 git commit 子模块:

git add src/repo
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,我需要做的就是从主存储库更新子模块本身。类似于:`git submodule update src/repo` (32认同)
  • 如果我不想在主存储库中添加任何提交怎么办?我的用例是 - 我有一个主存储库,然后我有它的 wiki,它也是一个存储库(在 GitHub 和 Bitbucket 中可用)。现在我已经在目录 **wiki** 中添加了 `wiki` 作为子模块。我不希望我对 `wiki`(即 **wiki** 目录)的任何更改反映在我的主/代码存储库中。我应该在主存储库的`.gitignore` 中添加`.gitmodules` **path** 吗?我该怎么办? (3认同)

Zak*_*Zak 51

我刚刚遇到了同一类问题,并且能够在已接受答案的评论部分使用@AugustinAmenabar提供的解决方案。我的设置有点复杂,所以我添加了--recursive标志以使所有依赖项都是最新的。

git submodule update --recursive src/repo

  • 对于当前 git 版本(我有 2.24),标志 `--recursive` 标志必须在子模块路径 `git submodule update --recursive src/repo` 之前。 (3认同)