jpskill.com
🛠️ 開発・MCP コミュニティ

git-submodule

Manage Git submodules for including external repositories within a main repository. Use when working with external libraries, shared modules, or managing dependencies as separate Git repositories.

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o git-submodule.zip https://jpskill.com/download/20904.zip && unzip -o git-submodule.zip && rm git-submodule.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/20904.zip -OutFile "$d\git-submodule.zip"; Expand-Archive "$d\git-submodule.zip" -DestinationPath $d -Force; ri "$d\git-submodule.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して git-submodule.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → git-submodule フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

[スキル名] git-submodule

Git Submodule

このスキルを使用する場面

  • メインプロジェクト内に外部のGitリポジトリを含める場合
  • 複数のプロジェクト間で共有ライブラリやモジュールを管理する場合
  • 外部依存関係を特定のバージョンに固定する場合
  • 独立したコンポーネントを持つモノレポスタイルのアーキテクチャで作業する場合
  • サブモジュールを含むリポジトリをクローンする場合
  • サブモジュールを新しいバージョンに更新する場合
  • プロジェクトからサブモジュールを削除する場合

手順

ステップ1: サブモジュールの理解

Git submoduleは、メインのGitリポジトリ内に他のGitリポジトリを含めるための機能です。

主要な概念:

  • サブモジュールは特定のコミットを参照することでバージョンを固定します。
  • サブモジュールのパスとURLは.gitmodulesファイルに記録されます。
  • サブモジュール内の変更は、個別のコミットとして管理されます。

ステップ2: サブモジュールの追加

基本的な追加:

# サブモジュールを追加
git submodule add <repository-url> <path>

# 例: libs/libパスにライブラリを追加
git submodule add https://github.com/example/lib.git libs/lib

特定のブランチを追跡:

# 特定のブランチを追跡するように追加
git submodule add -b main https://github.com/example/lib.git libs/lib

追加後のコミット:

git add .gitmodules libs/lib
git commit -m "feat: add lib as submodule"

ステップ3: サブモジュールを含むクローン

新規クローン時:

# 方法1: クローン時に --recursive オプションを使用
git clone --recursive <repository-url>

# 方法2: クローン後に初期化
git clone <repository-url>
cd <repository>
git submodule init
git submodule update

1行で初期化と更新:

git submodule update --init --recursive

ステップ4: サブモジュールの更新

最新のリモートバージョンに更新:

# すべてのサブモジュールを最新のリモートに更新
git submodule update --remote

# 特定のサブモジュールのみを更新
git submodule update --remote libs/lib

# 更新 + マージ
git submodule update --remote --merge

# 更新 + リベース
git submodule update --remote --rebase

参照されているコミットにチェックアウト:

# メインリポジトリが参照するコミットにサブモジュールをチェックアウト
git submodule update

ステップ5: サブモジュール内での作業

サブモジュール内での作業:

# サブモジュールディレクトリに移動
cd libs/lib

# ブランチをチェックアウト (detached HEADを解消)
git checkout main

# 変更作業
# ... 変更を行う ...

# サブモジュール内でコミットとプッシュ
git add .
git commit -m "feat: update library"
git push origin main

メインリポジトリにサブモジュールの変更を反映:

# メインリポジトリに移動
cd ..

# サブモジュールの参照を更新
git add libs/lib
git commit -m "chore: update lib submodule reference"
git push

ステップ6: バッチ操作

すべてのサブモジュールでコマンドを実行:

# すべてのサブモジュールでプルを実行
git submodule foreach 'git pull origin main'

# すべてのサブモジュールでステータスを確認
git submodule foreach 'git status'

# すべてのサブモジュールでブランチをチェックアウト
git submodule foreach 'git checkout main'

# ネストされたサブモジュールでもコマンドを実行
git submodule foreach --recursive 'git fetch origin'

ステップ7: サブモジュールの削除

サブモジュールを完全に削除:

# 1. サブモジュールを初期化解除
git submodule deinit <path>

# 2. Gitから削除
git rm <path>

# 3. .git/modulesからキャッシュを削除
rm -rf .git/modules/<path>

# 4. 変更をコミット
git commit -m "chore: remove submodule"

例: libs/libを削除:

git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib
git commit -m "chore: remove lib submodule"
git push

ステップ8: サブモジュールのステータス確認

ステータスを確認:

# サブモジュールのステータスを確認
git submodule status

# 詳細なステータス (再帰的)
git submodule status --recursive

# 要約情報
git submodule summary

出力の解釈:

 44d7d1... libs/lib (v1.0.0)      # 通常 (参照されているコミットと一致)
+44d7d1... libs/lib (v1.0.0-1-g...)  # ローカルに変更がある
-44d7d1... libs/lib               # 初期化されていない

例1: プロジェクトに外部ライブラリを追加する

# 1. サブモジュールを追加
git submodule add https://github.com/lodash/lodash.git vendor/lodash

# 2. 特定のバージョン (タグ) に固定
cd vendor/lodash
git checkout v4.17.21
cd ../..

# 3. 変更をコミット
git add .
git commit -m "feat: add lodash v4.17.21 as submodule"

# 4. プッシュ
git push origin main

例2: サブモジュールを含むリポジトリをクローンした後のセットアップ

# 1. リポジトリをクローン
git clone https://github.com/myorg/myproject.git
cd myproject

# 2. サブモジュールを初期化して更新
git submodule update --init --recursive

# 3. サブモジュールのステータスを確認
git submodule status

# 4. サブモジュールのブランチをチェックアウト (開発用)
git submodule foreach 'git checkout main || git checkout master'

例3: サブモジュールを最新バージョンに更新する

# 1. すべてのサブモジュールを最新のリモートに更新
git submodule update --remote --merge

# 2. 変更を確認
git diff --submodule

# 3. 変更をコミット
git add .
git commit -m "chore: update all submodules to latest"

# 4. プッシュ
git push origin main

例4: 複数のプロジェクト間で共有コンポーネントを使用する

# プロジェクトAで
git submodule add https://github.com/myorg/shared-components.git src/shared

# プロジェクトBで
git submodule add https://github.com/myorg/shared-components.git src/shared

# 共有コンポーネントを更新する場合 (各プロジェクトで)
git submodule update --remote src/shared
git add src/shared
git commit -m "chore: update shared-components"

例5: CI/CDでのサブモジュールの扱い

# GitHub Actions
jobs:
  build:
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive  # または 'true'

# GitLab CI
variables:
  GIT_SUBMODULE_STRATEGY: recursive

# Jenkins
checkout scm: [
  $class: 'SubmoduleOption',
  recursiveSubmodules: true
]

高度なワークフロー

ネストされたサブモジュール

# すべてのネストされたサブモジュールを初期化
git submodule update --init --recursive

# すべてのネストされたサブモジュールを更新
git submodule update --remo
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Git Submodule

When to use this skill

  • Including external Git repositories within your main project
  • Managing shared libraries or modules across multiple projects
  • Locking external dependencies to specific versions
  • Working with monorepo-style architectures with independent components
  • Cloning repositories that contain submodules
  • Updating submodules to newer versions
  • Removing submodules from a project

Instructions

Step 1: Understanding submodules

Git submodule is a feature for including other Git repositories within a main Git repository.

Key concepts:

  • Submodules lock version by referencing a specific commit
  • Submodule paths and URLs are recorded in the .gitmodules file
  • Changes within a submodule are managed as separate commits

Step 2: Adding submodules

Basic addition:

# Add submodule
git submodule add <repository-url> <path>

# Example: Add library to libs/lib path
git submodule add https://github.com/example/lib.git libs/lib

Track a specific branch:

# Add to track a specific branch
git submodule add -b main https://github.com/example/lib.git libs/lib

Commit after adding:

git add .gitmodules libs/lib
git commit -m "feat: add lib as submodule"

Step 3: Cloning with submodules

When cloning fresh:

# Method 1: --recursive option when cloning
git clone --recursive <repository-url>

# Method 2: Initialize after cloning
git clone <repository-url>
cd <repository>
git submodule init
git submodule update

Initialize and update in one line:

git submodule update --init --recursive

Step 4: Updating submodules

Update to latest remote version:

# Update all submodules to latest remote
git submodule update --remote

# Update a specific submodule only
git submodule update --remote libs/lib

# Update + merge
git submodule update --remote --merge

# Update + rebase
git submodule update --remote --rebase

Checkout to the referenced commit:

# Checkout submodule to the commit referenced by the main repository
git submodule update

Step 5: Working inside submodules

Working inside a submodule:

# Navigate to submodule directory
cd libs/lib

# Checkout branch (exit detached HEAD)
git checkout main

# Work on changes
# ... make changes ...

# Commit and push within submodule
git add .
git commit -m "feat: update library"
git push origin main

Reflect submodule changes in main repository:

# Move to main repository
cd ..

# Update submodule reference
git add libs/lib
git commit -m "chore: update lib submodule reference"
git push

Step 6: Batch operations

Run commands on all submodules:

# Pull in all submodules
git submodule foreach 'git pull origin main'

# Check status in all submodules
git submodule foreach 'git status'

# Checkout branch in all submodules
git submodule foreach 'git checkout main'

# Also run command on nested submodules
git submodule foreach --recursive 'git fetch origin'

Step 7: Removing submodules

Completely remove a submodule:

# 1. Deinitialize submodule
git submodule deinit <path>

# 2. Remove from Git
git rm <path>

# 3. Remove cache from .git/modules
rm -rf .git/modules/<path>

# 4. Commit changes
git commit -m "chore: remove submodule"

Example: Remove libs/lib:

git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib
git commit -m "chore: remove lib submodule"
git push

Step 8: Checking submodule status

Check status:

# Check submodule status
git submodule status

# Detailed status (recursive)
git submodule status --recursive

# Summary information
git submodule summary

Interpreting output:

 44d7d1... libs/lib (v1.0.0)      # Normal (matches referenced commit)
+44d7d1... libs/lib (v1.0.0-1-g...)  # Local changes present
-44d7d1... libs/lib               # Not initialized

Examples

Example 1: Adding an External Library to a Project

# 1. Add submodule
git submodule add https://github.com/lodash/lodash.git vendor/lodash

# 2. Lock to a specific version (tag)
cd vendor/lodash
git checkout v4.17.21
cd ../..

# 3. Commit changes
git add .
git commit -m "feat: add lodash v4.17.21 as submodule"

# 4. Push
git push origin main

Example 2: Setup After Cloning a Repository with Submodules

# 1. Clone the repository
git clone https://github.com/myorg/myproject.git
cd myproject

# 2. Initialize and update submodules
git submodule update --init --recursive

# 3. Check submodule status
git submodule status

# 4. Checkout submodule branch (for development)
git submodule foreach 'git checkout main || git checkout master'

Example 3: Updating Submodules to the Latest Version

# 1. Update all submodules to latest remote
git submodule update --remote --merge

# 2. Review changes
git diff --submodule

# 3. Commit changes
git add .
git commit -m "chore: update all submodules to latest"

# 4. Push
git push origin main

Example 4: Using Shared Components Across Multiple Projects

# In Project A
git submodule add https://github.com/myorg/shared-components.git src/shared

# In Project B
git submodule add https://github.com/myorg/shared-components.git src/shared

# When updating shared components (in each project)
git submodule update --remote src/shared
git add src/shared
git commit -m "chore: update shared-components"

Example 5: Handling Submodules in CI/CD

# GitHub Actions
jobs:
  build:
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive  # or 'true'

# GitLab CI
variables:
  GIT_SUBMODULE_STRATEGY: recursive

# Jenkins
checkout scm: [
  $class: 'SubmoduleOption',
  recursiveSubmodules: true
]

Advanced workflows

Nested Submodules

# Initialize all nested submodules
git submodule update --init --recursive

# Update all nested submodules
git submodule update --remote --recursive

Changing Submodule URL

# Edit the .gitmodules file
git config -f .gitmodules submodule.libs/lib.url https://new-url.git

# Sync local configuration
git submodule sync

# Update submodule
git submodule update --init --recursive

Converting a Submodule to a Regular Directory

# 1. Back up submodule contents
cp -r libs/lib libs/lib-backup

# 2. Remove submodule
git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib

# 3. Restore backup (excluding .git)
rm -rf libs/lib-backup/.git
mv libs/lib-backup libs/lib

# 4. Add as regular files
git add libs/lib
git commit -m "chore: convert submodule to regular directory"

Saving Space with Shallow Clones

# Add submodule with shallow clone
git submodule add --depth 1 https://github.com/large/repo.git libs/large

# Update existing submodule as shallow clone
git submodule update --init --depth 1

Best practices

  1. Version locking: Always lock submodules to a specific commit/tag for reproducibility
  2. Documentation: Specify submodule initialization steps in README
  3. CI configuration: Use --recursive option in CI/CD pipelines
  4. Regular updates: Regularly update submodules for security patches and more
  5. Branch tracking: Configure branch tracking during development for convenience
  6. Permission management: Verify access permissions for submodule repositories
  7. Shallow clone: Use --depth option for large repositories to save space
  8. Status check: Verify status with git submodule status before committing

Common pitfalls

  • detached HEAD: Submodules are in detached HEAD state by default. Checkout a branch when working
  • Missing initialization: git submodule update --init is required after cloning
  • Reference mismatch: Must update reference in main repository after submodule changes
  • Permission issue: Private submodules require SSH key or token configuration
  • Relative paths: Using relative paths in .gitmodules can cause issues in forks
  • Incomplete removal: Must also delete .git/modules cache when removing a submodule

Troubleshooting

Submodule not initialized

# Force initialize
git submodule update --init --force

Submodule conflict

# Check submodule status
git submodule status

# After resolving conflict, checkout desired commit
cd libs/lib
git checkout <desired-commit>
cd ..
git add libs/lib
git commit -m "fix: resolve submodule conflict"

Permission error (private repository)

# Use SSH URL
git config -f .gitmodules submodule.libs/lib.url git@github.com:org/private-lib.git
git submodule sync
git submodule update --init

Submodule in dirty state

# Check changes within submodule
cd libs/lib
git status
git diff

# Discard changes
git checkout .
git clean -fd

# Or commit
git add .
git commit -m "fix: resolve changes"
git push

Configuration

Useful Configuration

# Show submodule changes in diff
git config --global diff.submodule log

# Show submodule summary in status
git config --global status.submoduleSummary true

# Check submodule changes on push
git config --global push.recurseSubmodules check

# Also fetch submodules when fetching
git config --global fetch.recurseSubmodules on-demand

.gitmodules Example

[submodule "libs/lib"]
    path = libs/lib
    url = https://github.com/example/lib.git
    branch = main

[submodule "vendor/tool"]
    path = vendor/tool
    url = git@github.com:example/tool.git
    shallow = true

References