Git submodule shows new commits, submodule status says nothing to commit

git

In a git repository, I have set up my .gitmodules file to reference a github repository:

[submodule "src/repo"]
    path = src/repo
    url = repourl

when I 'git status' on this repo, it shows:

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)

If I cd into src/repo and git status on repo, it says that there is nothing to commit.

Why is my top-level git repo complaining?

Best Answer

It's because Git records which commit (not a branch or a tag, exactly one commit represented in SHA-1 hash) should be checked out for each submodule. If you change something in submodule dir, Git will detect it and urge you to commit those changes in the top-level repoisitory.

Run git diff in the top-level repository to show what has actually changed Git thinks. If you've already made some commits in your submodule (thus "clean" in submodule), it reports submodule's hash change.

$ 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

Otherwise it shows -dirty hash change which you cannot stage or commit in the top-level repository. git status also claims submodule has untracked/modified content.

$ 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")

To update which commit records should be checked out for the submodule, you need to git commit the submodule in addition to committing the changes in the submodule:

git add src/repo
Related Question