# Commands about Git Submodule
#### How to list all submodules in project?
```bash=
git submodule
```
#### How to clone a project including submodules?
```bash=
git clone <repository> <path>
# enter the repo folder.
cd <path>
# update submodules in project
git submodule update --init
```
#### How to get a clean project including submodules while switching a branch?
```bash=
# Deinit all submodules so there will leave nothing about submodules
# after switch the branch.
git submodule deinit .
# Switch to your target branch.
git checkout <branch>
# Pull all submodules.
git submodule update --init
```
#### How to list all status about submodules except specific one?
```bash=
# Keyword *foreach means that all submodules in this project will do something.
git submodule foreach 'case $name in <path>) ;; *) git status ;; esac'
```
#### How to make a bulk change with several submodules?
```bash=
# There might occur errors, such as the working space is dirty or something else,
# you need to resolve,
# but it's still a convenient command for a bulk modification.
git submodule foreach 'git add .; git commit -m <msg>; git push'
```
#### How to list the settings about submodules?
```bash=
git config --file=.gitmodules -l
```
#### How to add a submodule?
```bash=
git submodule add -b <branch> -- <repository> [<path>]
```
#### How to change the remote for a submodule?
```bash=
# Git will syna automatically after change the settings via git submodule.
git submodule set-url -- <path> <repository>
# Instead of command, you can modify .gitmodules file directly.
# After the modification you need to execute the command below to sync settings.
git submodule sync
```
#### How to change tracking remote branch for a submodule?
```bash=
git submodule set-branch -b <branch> -- <path>
```
#### How to totally remove a submodule in the project?
```bash=
# Remove the submodule entry from .git/config
git submodule deinit -f -- <path>
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/<path>
# in powershell: rm -Recurse -Force .git/modules/<path>
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f <path>
```
#### How to totally remove all submdoules?
```bash=
# deinit all submodules from .gitmodules
git submodule deinit -f .
# remove all submodules (`git rm`) from .gitmodules
git submodule | cut -c43- | while read -r line; do (git rm "$line"); done
# delete all submodule sections from .git/config (`git config --local --remove-section`) by fetching those from .git/config
git config --local -l | grep submodule | sed -e 's/^\(submodule\.[^.]*\)\(.*\)/\1/g' | while read -r line; do (git config --local --remove-section "$line"); done
# manually remove leftovers
rm .gitmodules
rm -rf .git/modules
```
###### tags: `Git` `Submodule`