Skip to main content

Git Submodule

Adding or Cloning a submodule

git submodule add <url>

Update All Submodules (no local changes)

If you do not have any local changes in your submodules, and all you want to do is consume new commits for your submodules' upstreams, you can run this command. It will go through each submodule, update remotes, and then update to the latest commit.

git submodule update --remote [--recursive]

Update All Submodules (with local changes)

If you have made local changes to your submodules, and want to pull new changes from the submodules' upstream, git makes this really simple. Make sure to specify whether you want to rebase or merge.

git submodule update --remote [--rebase | --merge] [--recursive]

Another Way to Update All Submodule

git submodule foreach --recursive git checkout master
git submodule foreach --recursive git pull

Push Local Work to Submodule

A submodule is nothing but a clone of a git repo within another repo with some extra metadata (gitlink tree entry, .gitmodules file )

cd your_submodule
git checkout master
git commit -a -m "commit in submodule"
git push
cd ..
git add your_submodule
git commit -m "Updated submodule"

Common Error

fatal: Needed a single revision Unable to find current origin/master revision in submodule path 'xxFolder' Fix by remove the folder and update again

rm -rf xxFolder
git submodule update

Removing a submodule

git submodule deinit path/to/module 
git rm path/to/module
git commit -am "Removed submodule X"

[[basic-git-command]]