Git update submodules recursively
GitGit SubmodulesGit Problem Overview
My project struture
ProjectA
-FrameworkA (submodule)
--Twig (submodule of FrameworkA)
How I can update submodules recursively? I already tried some git commands (on ProjectA root)
git submodule foreach git pull origin master
or
git submodule foreach --recursive git pull origin master
but cannot pull files of Twig.
Git Solutions
Solution 1 - Git
git submodule update --recursive
You will also probably want to use the --init option which will make it initialize any uninitialized submodules:
git submodule update --init --recursive
Note: in some older versions of Git, if you use the --init
option, already-initialized submodules may not be updated. In that case, you should also run the command without --init
option.
Solution 2 - Git
The way I use is:
git submodule update --init --recursive
git submodule foreach --recursive git fetch
git submodule foreach git merge origin master
Solution 3 - Git
As it may happens that the default branch of your submodules are not master
(which happens a lot in my case), this is how I automate the full Git submodules upgrades:
git submodule init
git submodule update
git submodule foreach 'git fetch origin; git checkout $(git rev-parse --abbrev-ref HEAD); git reset --hard origin/$(git rev-parse --abbrev-ref HEAD); git submodule update --recursive; git clean -dfx'
Solution 4 - Git
In recent Git (I'm using v2.15.1), the following will merge upstream submodule changes into the submodules recursively:
git submodule update --recursive --remote --merge
You may add --init
to initialize any uninitialized submodules and use --rebase
if you want to rebase instead of merge.
You need to commit the changes afterwards:
git add . && git commit -m 'Update submodules to latest revisions'
Solution 5 - Git
You can add the following to your Makefile
:
submodule:
git submodule update --init --recursive
git submodule foreach 'git fetch origin; git checkout $$(git rev-parse --abbrev-ref HEAD); git reset --hard origin/$$(git rev-parse --abbrev-ref HEAD); git submodule update --recursive; git clean -dfx'
Then you can simple run make submodule
everytime you want to update submodules.
Solution 6 - Git
I had one submodule causing issues (the 'fatal:...' that Sanandrea reported, above). Navigated to the submodule and used 'git clean -dfx' resolved it.