Todays strategy to keep git submodule checkouts in sync with commit-IDs?
Asked Answered
V

1

11

A lot of questions about automatically update submodules have been asked on StackOverflow including:

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:

  • it's complicated and probably needs a script to make it working reliably
  • does not work well if you use these git-hooks already
  • the commit hooks are only active on this clone (has to be re-done by everyone working on this project on every single clone)
  • setup is non-standard and will confuse others

#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:

  • it won't --init the submodules (can be solved by running pull/checkout and submodule update separately instead
  • it's non-standard and won't work with scripts and snippets
  • it's easy to forget to use up/co instead of pull/checkout
  • it works only on the local machine/user

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?

Vladikavkaz answered 9/10, 2017 at 10:28 Comment(0)
P
3

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.

Palembang answered 12/5, 2020 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.