Skip to content

Instantly share code, notes, and snippets.

@colematt
Last active July 23, 2020 23:13
Show Gist options
  • Select an option

  • Save colematt/aa58c5d7fdd63f5dfa623d6b9317a14f to your computer and use it in GitHub Desktop.

Select an option

Save colematt/aa58c5d7fdd63f5dfa623d6b9317a14f to your computer and use it in GitHub Desktop.
[Working with Git submodules] #git

Cloning a repository with submodules

git clone --recurse-submodules <repository>

Updating submodules

Update submodules recursively (and/or initialize them if you haven't yet):

git submodule update --recursive
git submodule update --init --recursive

Update submodule to a later upstream commit:

  1. Change directory to the submodule's subdirectory.
  2. Checkout desired branch or commit.
  3. Update.
  4. Get back to your project root
  5. Make a commit for the changes
cd submodule_dir
git checkout master
git pull
cd ..
git commit -am "Pulled down update to submodule_dir"

... Or, if you're a busy person:

git submodule foreach git pull origin master

Adding a submodule

Add a submodule at <repository>, place it at <path> in this repository, and initialize it:

git submodule add <repository> [<path>]
cd <path>
git submodule init

Removing a submodule

  1. Delete the relevant line from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached pathtosubmodule (no trailing slash).
  4. Commit the superproject.
  5. Delete the now untracked submodule files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment