Advanced Git
Amanda Crawford-Adamo
Software and Data Engineer
Git Submodule
git submodule
Adding a submodule using the link or directory under path folder.
git submodule add <repository link|dir> <path>
$$
Example
Adds the data validator library to the ETL project under the libs/validator
folder in the ETL repo.
git submodule add https://github.com/example/data-validator.git libs/validator
List all submodules in a project
git submodule status
Example
$ git submodule status
e1f2...7w8x9 data_cleaning_lib
a1b2...q7r8 api_connector
d9e8...t3u2 visualization_toolkit
Update submodule with the latest changes
There are several options:
git submodule update --init
git submodule update --init --remote
git submodule update --init <path_to_submodule>
Remove a submodule process
git submodule deinit <submodule_name>
git rm <path>
Copy all files that need to be in the new submodule repo into another folder outside the repo.
Inside the new folder, create a new repository for the submodule:
git init <new-submodule>
Use git filter-repo to extract the relevant files and history from the main project:
git filter-repo --path <extract_path> --invert-paths
Add the extracted repository as a submodule to the main project:
git submodule add <new-submodule_path> <path_to_store_submodule>
Use cases:
Best practices:
Advanced Git