A lot of questions about automatically update submodules have been asked on StackOverflow including:
git checkout
automatically do git submodule update --recursive
?But it looks to me as for git submodules there is no single approach yet which works like svn up
regarding svn-externals.
So since git is changing every day I dare to ask again:
Is there a (convenient) way to init and automatically update submodule checkouts (i.e. keep in sync with their corresponding submodule commit IDs) for checkout
and pull
(i.e. merge
and rebase
)?
Currently I have two approaches for this:
#1: create a post-checkout
, post-merge
and post-rewrite
hook with the following content
#!/bin/sh
git submodule update --init --recursive
as you can already see this approach has several disadvantages:
#2: configure aliases for pull
and checkout
git config --global alias.up 'pull --recurse-submodules'
git config --global alias.co 'checkout --recurse-submodules'
But this isn't nice neither:
--init
the submodules (can be solved by running pull
/checkout
and submodule update
separately insteadup
/co
instead of pull
/checkout
This approach would be a bit more like I want it to be if you could do something like
git config --global pull.recurseSubmodules true
git config --global pull.initSubmodules true
git config --global checkout.recurseSubmodules true
git config --global checkout.initSubmodules true
.. but you can't, do you?
Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated.
git submodule sync synchronizes all submodules while git submodule sync -- A synchronizes submodule "A" only. If --recursive is specified, this command will recurse into the registered submodules, and sync any nested submodules within.
If you track branches in your submodules, you can update them via the --remote parameter of the git submodule update command. This pulls in new commits into the main repository and its submodules. It also changes the working directories of the submodules to the commit of the tracked branch.
From the git-checkout manual page: Using --recurse-submodules will update the content of all initialized submodules according to the commit recorded in the superproject. If local modifications in a submodule would be overwritten the checkout will fail unless -f is used.
Using the --recurse-submodules flag of git checkout can also be useful when you work on several branches in the superproject, each having your submodule pointing at different commits.
In that case, it is possible for git pull --recurse-submodules, or git submodule update, to fail if the superproject references a submodule commit that is not found in the submodule remote locally configured in your repository. In order to remedy this situation, the git submodule sync command is required:
If local modifications in a submodule would be overwritten the checkout will fail unless -f is used. If nothing (or --no-recurse-submodules) is used, the work trees of submodules will not be updated. See also this relevant git mailing list message about that new option.
Since Git 2.13, you can do git checkout --recurse-submodules <ref>
which will make sure that the working tree(s) of active submodule(s) are in sync with the submodule commit(s) recorded in the superproject at <ref>
.
Since Git 2.14, you can do git pull --recurse-submodules
, which will do a normal pull and will then invoke git submodule update --init --recursive
(with --rebase
if you pulled with --rebase
), keeping active submodule(s) working tree(s) in sync with the submodule commit(s) recorded in the superproject.
Since Git 2.15, setting git config submodule.recurse true
will make this the default behaviour for all commands that accept the --recurse-submodules
flag, except clone
and ls-files
.
There is no way to make this behaviour the default without changing your configuration or using these options.
Note that doing git checkout --recurse-submodules <branch>
when the current branch has no submodules and <branch>
has initialized nested submodules fails before Git 2.27.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With