Gitサブモジュール

高度な Git

Amanda Crawford-Adamo

Software and Data Engineer

Gitサブモジュールとは

Gitサブモジュール

git submodule
  • 別リポジトリをリポジトリ内にネスト
  • それぞれ独立した履歴とバージョン管理
  • サブモジュールの変更はメインに影響しない
  • メインはサブモジュールの特定バージョンを参照可能

複数のリポジトリを内包するリポジトリの図。

高度な Git

サブモジュールの追加

パス配下にあるリンクまたはディレクトリを指定してサブモジュールを追加します。

git submodule add <repository link|dir> <path>

$$

ETLプロジェクトの libs/validator 配下に data validator ライブラリを追加します。

git submodule add https://github.com/example/data-validator.git libs/validator
高度な Git

サブモジュールの一覧表示

プロジェクト内のサブモジュール一覧

git submodule status

$ git submodule status
e1f2...7w8x9 data_cleaning_lib
a1b2...q7r8 api_connector
d9e8...t3u2 visualization_toolkit
高度な Git

サブモジュールの更新

最新の変更でサブモジュールを更新

方法はいくつかあります:

  1. ソースがローカルにある全サブモジュールを更新。
    git submodule update --init 
    
  2. ソースがリモートにある全サブモジュールを更新。
    git submodule update --init --remote 
    
  3. 特定のサブモジュールのみ更新。
    git submodule update --init <path_to_submodule>
    
高度な Git

サブモジュールの削除

サブモジュール削除の手順

  1. サブモジュールを初期化解除。
    git submodule deinit <submodule_name>
    
  2. Gitのインデックスから削除。
    git rm <path>
    
高度な Git

大規模リポジトリからのサブモジュール抽出

  1. 新しいサブモジュール用に必要なファイルを、リポジトリ外の別フォルダへコピーします。

  2. そのフォルダ内でサブモジュール用の新規リポジトリを作成:

    git init <new-submodule>
    
  3. git filter-repo でメインから対象ファイルと履歴を抽出:

    git filter-repo --path <extract_path> --invert-paths
    
  4. 抽出したリポジトリをメインにサブモジュールとして追加:

    git submodule add <new-submodule_path> <path_to_store_submodule>
    
高度な Git

サブモジュールの利用場面とベストプラクティス

ユースケース:

  1. 外部ライブラリの管理
  2. 複数プロジェクトでのコード共有
  3. 依存関係の特定バージョン維持

ベストプラクティス:

  1. サブモジュールを最新に保つ
  2. 相対パスを使用
  3. 変更をチームと共有
高度な Git

Let's practice!

高度な Git

Preparing Video For Download...