1

I am seeing here # https://github.com/UCSD-PL/proverbot9001/issues/73 :

# run git submodule update and the && makes sure init is only ran if the first worked
git submodule update && git submodule init  
# https://github.com/UCSD-PL/proverbot9001/issues/73

But I believe this is the right/safer/more standard way:


# - git submodule init initializes your local configuration file to track the submodules your repository uses, it just sets up the configuration so that you can use the git submodule update command to clone and update the submodules.
git submodule init
# - The --remote option tells Git to update the submodule to the commit specified in the upstream repository, rather than the commit specified in the main repository. ref: https://stackoverflow.com/questions/74988223/why-do-i-need-to-add-the-remote-to-gits-submodule-when-i-specify-the-branch?noredirect=1&lq=1
git submodule update --init --recursive --remote
# - for each submodule pull from the right branch according to .gitmodule file. ref: https://stackoverflow.com/questions/74988223/why-do-i-need-to-add-the-remote-to-gits-submodule-when-i-specify-the-branch?noredirect=1&lq=1
git submodule foreach -q --recursive 'git switch $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master || echo main )'
# - check it's in specified branch. ref: https://stackoverflow.com/questions/74998463/why-does-git-submodule-status-not-match-the-output-of-git-branch-of-my-submodule
git submodule status

am I right? I'd love to be corrected or know the good practices for this.


cross:

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323

2 Answers2

3

A simpler instruction for the UCSD-PL/proverbot9001 repository, which references a lot of submodules (cf. its .gitmodules file), would be to just use the git clone --recurse-submodules option:

That would do everything: clone the parent repository, initialize all submodules and update them. All in one go.

As explained in "What is the point of 'git submodule init'?", separating both steps (init and update) makes sense when you do not want all submodules (because it would take too much time/resource to clone them), and want only to work on a subset.

In that case, yes, init must comes before update.

This comes from commit be4d2c8, Git v1.5.6-rc0, May 2008

submodule update: add convenience option --init

When a submodule is not initialized and you do not want to change the defaults from .gitmodules anyway, you can now say

$ git submodule update --init <name>

When "update" is called without --init on an uninitialized submodule, a hint to use --init is printed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You're correct, init should be run before update.

As noted in the docs, init will

Initialize the submodules recorded in the index (which were added and committed elsewhere) by setting submodule.$name.url in .git/config. It uses the same setting from .gitmodules as a template.

In other words, init sets up the configuration which will be used by update. If you're not making any local modifications to that config then you can also just run update --init. See the docs

If the submodule is not yet initialized, and you just want to use the setting as stored in .gitmodules, you can automatically initialize the submodule with the --init option.

Calum Halpin
  • 1,945
  • 1
  • 10
  • 20