Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git submodule has "new commits" when I checkout master

I have a submodule called Helpers. When I clone my main project with --recursive, the Helpers submodule is in a detached head state (as all of the tutorials say it should be). If I now 'git status' in the main project directory, everything is clean. If I 'cd Helpers; git checkout master', I would expect nothing to change except that I am now on a named branch that I can commit to. However, without doing anything else, if I 'cd ..; git status', I see

 modified:   Helpers (new commits)

Why does it think there are new commits? The submodule should still be at the same point as when it was updated (in this case, cloned), no?

like image 527
David Doria Avatar asked Apr 28 '15 20:04

David Doria


1 Answers

What is probably happening is that your submodule's master is not the version that your holding repo wants.

Set your submodule to the version that the holding repo wants:

holding/ $ git submodule update

Check the desired SHA1 of the submodule:

holding/ $ git submodule        # (1)
<list of all submodules, with the desired SHA1>

Check what your submodule's master is:

holding/ $ cd sub
holding/sub $ git checkout master
holding/sub $ git log -1     # (2)

Is the SHA1 at (2) the same as what you saw at (1)? If not, that is your problem. Something happened in the submodule's master, but these new changes were not included in the holding repo.

The holding repo keeps a SHA1 as reference to the version of the submodule to use. If further development occurs in the submodule's repo, the holding repo still keeps the same version, it does not automatically update the submodule.

If you checkout a newer version (eg master) of the submodule and then go back to the holding repo, git status will tell you that you have checked out a new commit in your submodule. This change of current submodule commit can be commited in your holding repo. That would update which version of the submodule is to be used with this commit of holding.


If you want to continue work on the submodule, from the version that the holding repo wants, you will need to create a new branch. master reflects further development in the submodule's repo, so either you work from master (and include both this further development and your new work), or you create a new branch from the detached head that holding refers to.

If you were to force master on the detached head that is desired by holding, what would happen to the commits (further dev) that were created between the detached head and master? They would be lost. And what would happen next time you push your moved master to origin? There would be a conflict since your local master would have diverged from origin's.

like image 50
Gauthier Avatar answered Sep 22 '22 05:09

Gauthier