gitgit-submodulesgit-commit

Does the commit ID of a submodule in the parent repository automatically update after a new commit is made in the submodule?


I have a Git repository with a submodule, and after creating a new commit in the submodule repository, I noticed that the commit ID of the submodule in the parent repository automatically updated when I ran git submodule status in the parent repository.

$ git submodule update --init sub-repo
Submodule 'sub-repo' (git@github.com:xxx/sub-repo.git) registered for path 'sub-repo'
Cloning into '/Users/xxx/demo2/main-repo/sub-repo'...
Submodule path 'sub-repo': checked out 'b39a213109219d943efd5f0d50a765344760a5cd'
$ git submodule status
 b39a213109219d943efd5f0d50a765344760a5cd sub-repo (heads/main)

$ cd sub-repo
$ touch file2
$ git add .
$ git commit -m "add file2"
[detached HEAD 8bfc422] add file2
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2
$ cd ..
$ git submodule status
+8bfc4220fbd6c941e493bfa790d049c7169514f9 sub-repo (heads/main-1-g8bfc422)  # commit ID changed?

Does the commit ID of the submodule in the parent repository automatically update after a new commit is made in the submodule? If so, what triggers this update?


Solution

  • $ git submodule status
    +8bfc4220fbd6c941e493bfa790d049c7169514f9 sub-repo (heads/main-1-g8bfc422)
    

    The status has + at the beginning which means the currently checked out submodule commit does not match the SHA-1 found in the index of the superproject. You should update the superproject:

    $ git add sub-repo
    $ git commit -m "Update submodule sub-repo"