Is there a way to make git pull automatically update submodules?
Is there a way to automatically have git submodule update
(or preferably git submodule update --init
called whenever git pull
is done?
Looking for a git config setting, or a git alias to help with this.
As of Git 2.14, you can use git pull --recurse-submodules
(and alias it to whatever you like).
As of Git 2.15, you could set submodule.recurse
to true to enable the desired behaviour.
You can do this globally by running:
git config --global submodule.recurse true
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
If you want arguments to be passed to git pull, then use this instead:
git config --global alias.pullall '!f(){ git pull "$@" && git submodule update --init --recursive; }; f'
Starting with Git 1.7.5 it should update submodules automatically by default like you want it to.
[EDIT: per comments: the new 1.7.5 behaviour is to automatically fetch the latest commits for submodules, but not to update them (in the git submodule update
sense). So the information in this answer is relevant as background, but is not a complete answer by itself. You still need an alias to pull and update submodules in one command.]
The default behavior, "on-demand", is to update submodules whenever you fetch a commit that updates the submodule commit, and this commit isn't already located in your local clone.
You can also have it updated on every fetch or never (pre-1.7.5 behavior I assume).
The config option to change this behavior is fetch.recurseSubmodules
.
This option can be either set to a boolean value or to
on-demand
.
Setting it to a boolean changes the behavior offetch
andpull
to unconditionally recurse into submodules when set to true or to not recurse at all when set to false.When set to
on-demand
(the default value),fetch
andpull
will only recurse into a populated submodule when its superproject retrieves a commit that updates the submodule’s reference.
See:
-
git config
man page (1.7.5) (or latestgit config
man page) -
git fetch
man page (1.7.5) (or latest git fetch man page)
for more information.
git fetch --recurse-submodules[=yes|on-demand|no]
I'm surprised nobody mentioned using git hooks to do this!
Just add files named post-checkout
and post-merge
to your .git/hooks
directory of the relevant repositories, and put the following into each of them:
#!/bin/sh
git submodule update --init --recursive
Since you specfically asked for an alias, assuming you want to have this for many repositories, you can create an alias which adds these to a repository's .git/hooks
for you.