Git Submodules: Automatic init and update on fetch and pull

NOTE:

You still have to be careful to alwyas push changes made in the submodule/sub-project before you commit and push changes in the super-project.

Git 1.7.5 and later

#!/usr/bin/env bash
git clone --recursive -o name repository destination 
pushd destination
  git fetch --recurse-submodules=yes
  git checkout name branch
popd

Once cloned and abranch is checked out the usual update workflow is:

#!/usr/bin/env bash
pushd destination
  git pull --recurse-submodules=yes

  # Or

  git fetch --recurse-submodules=yes
popd

To make these the default behavior in a project, so you can use git pull and git fetch without the recurse option being given:

#!/usr/bin/env bash
git config fetch.recurseSubmodules yes

Git pre-1.7.5

The usual clone workflow for Git earlier than 1.7.5:

#!/usr/bin/env bash
git clone -o name repository destination
pushd destination
  git fetch
  git checkout name branch
  git submodule init
  git submodule update 
popd

Once cloned the usual update workflow is :

#!/usr/bin/env bash
pushd destination
  git pull name branch
  git submodule update
popd

For earlier versions of Git:

  • Create post-merge hook
  • Create an alias

    git config alias.pullall '!f(){ git pull "$@" && git submodule init && git submodule update --recursive; }; f'

Hope that helps.