Git Submodules By Example
Last updated:- Create submodule in existing git repo
- Initialize git submodule
- Update git submodule
- Update all git submodules in repo
- Remove git submodule
- Reset all submodules in git repo
- Clone repository with submodules
- Update repo and submodules together
- Enable submodule information in git status
Create submodule in existing git repo
This registers a submodule under your main repo, but files aren't actually downloaded.
Inside an existing git repository, do:
(this will create a directory called my-other-project
in the root directory of the outer repository)
main-repo$ git submodule add git://github.com/some-user/my-other-project.git
Initialize git submodule
Initialize (i.e. populate) a registered git submodule.
This is where files in submodules get actually downloaded.
Example: initialize a submodule called my-other-project
.
cd
to the submodule directory.my-other-project$ git submodule init
my-other-project$ git submodule update --remote
Update git submodule
In the submodule directory, do:
my-other-project$ git submodule update --remote
Update all git submodules in repo
In the outer repository, do:
main-repo$ git submodule update --remote
Remove git submodule
This will remove the submodule directory for <submodule_name>
and make git forget it ever existed:
main-repo$ git rm --cached <submodule_name>
Delete the relevant lines from file
.gitmodules
Delete the relevant section from file
.git/config
or runmain-repo$ git submodule deinit <submodule_name>
if on newer git versionsCommit the changes
Delete the now untracked submodule files
Remove directory
.git/modules/<submodule_name>
Reset all submodules in git repo
In the outer repository, do:
main-repo$ git submodule foreach --recursive git clean -xfd
main-repo$ git reset --hard
main-repo$ git submodule foreach --recursive git reset --hard
main-repo$ git submodule update --init --recursive
Clone repository with submodules
Example: clone repo repo.git
and initialize any submodules within.
$ git clone --recursive git@github.com:name/repo.git
Update repo and submodules together
This is what you need to do if you want to pull both a repository as well as all its submodules:
In the outer repository, do:
main-repo$ git pull origin master
main-repo$ git submodule sync --recursive
main-repo$ git submodule update --init --recursive
Enable submodule information in git status
Update git configuration as follows (in the outer repository):
main-repo$ git config --global status.submoduleSummary true