List of useful commands to work with submodules.
Add submodule
git submodule add https://github.com/*user*/*repo* path/to/submodule/directory
For using private repository as a submodule:
git submodule add [email protected]:*user*/*repo*.git path/to/submodule/directory
Example
git submodule add https://github.com/codex-team/editor.js editorjs
You will see changes in .gitmodules
file and submodule's directory. Commit changes in both files.
![](https://static.codex.so/upload/redactor_images/o_718547c288f207aaac4f6d414a9f4068.jpg)
Init submodules
After cloning a repository with submodules you need to initialize them. Otherwise you will see empty directories in submodules places.
Git will download submodules recursively.
git submodule update --init --recursive
Pull updates
Pull submodules by commited state.
git submodule update --recursive
Get the latest updates for remote branches
git submodule update --recursive --remote
Remove submodule
1. Delete the relevant section from the .gitmodules file.
-[submodule "editorjs"]
- path = editorjs
- url = https://github.com/codex-team/editor.js
![](https://static.codex.so/upload/redactor_images/o_0b1c90328b9b597aa427c13f4a098d30.jpg)
2. Stage the .gitmodules
changes: git add .gitmodules
.
3. Delete the relevant section from .git/config
.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/talyguryn/test
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
-[submodule "editorjs"]
- active = true
- url = https://github.com/codex-team/editor.js
4. Run git rm --cached path_to_submodule
(no trailing slash).
![](https://static.codex.so/upload/redactor_images/o_306ecbc6c7809918c2503060fa8e7f88.jpg)
5. Run rm -rf .git/modules/path_to_submodule
(no trailing slash).
![](https://static.codex.so/upload/redactor_images/o_fcf368b9e7747a1c423faf32eb319b2b.jpg)
6. Delete the now untracked submodule files rm -rf path_to_submodule
.
7. Commit changes: git commit -am "Remove submodule"
.
Working with submodules
You can work with any submodule via your git client (as a regular repository).
For example. Any changes inside submodule will be shown as dirty
state.
![](https://static.codex.so/upload/redactor_images/o_bc85d7bd87e581b85425321f6f987d8e.jpg)
Add submodule's directory as a local repository.
![](https://static.codex.so/upload/redactor_images/o_b324b40e3660b44adc48351482b1381c.jpg)
And you will see changes, commit them and checkout branches.
![](https://static.codex.so/upload/redactor_images/o_9f44af2b21e1b864073beccf54e70c6a.jpg)