I love using git submodules to avoid duplicating common code. Unfortunately, updating a submodule reference to the lastest revision is a chore:

cd framework
git checkout master
git pull
git submodule update --init --recursive
cd ..

However, we can store all of those commands using a git alias:

git config --global alias.up-sub '!f() { cd $1 && git checkout master && git pull && git submodule update --init --recursive; }; f'

Now, to update the ‘framework’ submodule, we can just do:

git up-sub framework

I picked up the trick for running multiple commands in an alias from this article on the Mozilla blog.

Update Use && instead of ; to prevent losing work when you mistype the submodule name.

Reactions (1)

Comments (3)

  • Mark Jaquith says:

    Even better, use git submodule foreach to do it for all submodules, fetch their tags, and don’t assume that you are using master… I check out WordPress a Git submodule, with a tag, for instance.

    git submodule foreach git fetch origin --tags && git pull && git submodule update --init --recursive

    • scribu says:

      When you first clone a repository that has submodules, they will be in detached HEAD state (no branch). Without a branch, git pull has no effect.

      Anyway, aliases are meant to speed up common operations, so to each his own.

      • Mark Jaquith says:

        Realized that right after I posted. :-)

        git submodule foreach 'git fetch origin --tags; git checkout master; git pull' && git pull && git submodule update --init --recursive

Respond / add a comment


Subscribe without commenting