Search code examples
gitgit-submodules

How do you initialize new submodules after a pull without affecting the pre-existing ones?


After pulling, I would like to initialize all new submodules recursively with a single command.

The issue with using git submodule update --init --recursive is that it also resets which commit my pre-existing submodules are pointing to.


Solution

  • Now you changed your question completely and you are asking (emphasis mine):

    After pulling, I would like to initialize all new submodules recursively with a single command.

    The issue with using git submodule update --init --recursive is that it also resets which commit my pre-existing submodules are pointing to.

    To initialize new submodules only, you first would have to find out if there are any new submodules that were brought by the git pull you did. Something like the following should list submodules (grep 160000) that have been added (--diff-filter=A) since the commit you were on before pulling (ORIG_HEAD..):

    git log --format=%h ORIG_HEAD.. --summary --diff-filter=A | \grep 160000 | awk '{print $4}'
    

    You could then use git submodule update with this list of submodules to only initialize these ones:

    git submodule update --init --recursive $(git log --format=%h ORIG_HEAD.. --summary --diff-filter=A | \grep 160000 | awk '{print $4}' | tr \\n ' ')
    

    Note that it's not clear from your question in what state are your pre-existing submodules, the ones that you want to avoid changing. Are they "modified" (i.e. you checked out a different commit in the submodule, but did not run git add in the superproject yet), "added" (i.e. you checked out a different commit in the submodule, ran git add in the superproject, but did not commit that change in the superproject yet) or if on the contrary your working directory is completely clean, but you did commit submodule changes since your last pull. Depending on that and what you want the end result to look like, simply running git submodule update --init --recursive --merge or git submodule update --init --recursive --rebase might fill your needs...