Git Submodules

Advanced Git

Amanda Crawford-Adamo

Software and Data Engineer

What is a Git Submodule?

Git Submodule

git submodule
  • A repository nested within another repository
  • Separate version control and history
  • Submodule changes does not affect main repo
  • Main repo can reference a specific version of a submodule

A depiction of a repo with multiple repository within it.

Advanced Git

Adding a 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
Advanced Git

Listing submodules

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
Advanced Git

Updating submodules

Update submodule with the latest changes

There are several options:

  1. Updates all submodules where the source code is on your local computer.
    git submodule update --init 
    
  2. Updates all submodule where the source code is on a remote repo.
    git submodule update --init --remote 
    
  3. Updates a specific submodules
    git submodule update --init <path_to_submodule>
    
Advanced Git

Removing submodules

Remove a submodule process

  1. Deinitialize the submodule.
    git submodule deinit <submodule_name>
    
  2. Remove the submodule from git repo index.
    git rm <path>
    
Advanced Git

Extracting a submodule from a large repo

  1. Copy all files that need to be in the new submodule repo into another folder outside the repo.

  2. Inside the new folder, create a new repository for the submodule:

    git init <new-submodule>
    
  3. Use git filter-repo to extract the relevant files and history from the main project:

    git filter-repo --path <extract_path> --invert-paths
    
  4. Add the extracted repository as a submodule to the main project:

    git submodule add <new-submodule_path> <path_to_store_submodule>
    
Advanced Git

When to use submodules and best practices

Use cases:

  1. Managing external libraries
  2. Sharing code across projects
  3. Maintaining specific versions of dependencies

Best practices:

  1. Keep submodules updated
  2. Use relative paths
  3. Communicate changes with team
Advanced Git

Let's practice!

Advanced Git

Preparing Video For Download...