重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
git-submodule by supercent-io/skills-template
npx skills add https://github.com/supercent-io/skills-template --skill git-submoduleGit 子模块是一项用于在主 Git 仓库中包含其他 Git 仓库的功能。
关键概念:
.gitmodules 文件中基本添加:
# 添加子模块
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
:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
git add .gitmodules libs/lib
git commit -m "feat: add lib as submodule"
初次克隆时:
# 方法 1:克隆时使用 --recursive 选项
git clone --recursive <repository-url>
# 方法 2:克隆后初始化
git clone <repository-url>
cd <repository>
git submodule init
git submodule update
一行命令初始化和更新:
git submodule update --init --recursive
更新到最新的远程版本:
# 将所有子模块更新到最新的远程版本
git submodule update --remote
# 仅更新特定的子模块
git submodule update --remote libs/lib
# 更新 + 合并
git submodule update --remote --merge
# 更新 + 变基
git submodule update --remote --rebase
检出到引用的提交:
# 将子模块检出到主仓库引用的提交
git submodule update
在子模块内部工作:
# 导航到子模块目录
cd libs/lib
# 检出分支(退出分离头指针状态)
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
在所有子模块上运行命令:
# 在所有子模块中拉取
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'
完全移除子模块:
# 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
检查状态:
# 检查子模块状态
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. 添加子模块
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
# 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'
# 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
# 在项目 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"
# 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 --remote --recursive
# 编辑 .gitmodules 文件
git config -f .gitmodules submodule.libs/lib.url https://new-url.git
# 同步本地配置
git submodule sync
# 更新子模块
git submodule update --init --recursive
# 1. 备份子模块内容
cp -r libs/lib libs/lib-backup
# 2. 移除子模块
git submodule deinit libs/lib
git rm libs/lib
rm -rf .git/modules/libs/lib
# 3. 恢复备份(排除 .git)
rm -rf libs/lib-backup/.git
mv libs/lib-backup libs/lib
# 4. 作为常规文件添加
git add libs/lib
git commit -m "chore: convert submodule to regular directory"
# 使用浅克隆添加子模块
git submodule add --depth 1 https://github.com/large/repo.git libs/large
# 将现有子模块更新为浅克隆
git submodule update --init --depth 1
--recursive 选项--depth 选项以节省空间git submodule status 验证状态git submodule update --init.gitmodules 中使用相对路径可能在分叉时导致问题.git/modules 缓存# 强制初始化
git submodule update --init --force
# 检查子模块状态
git submodule status
# 解决冲突后,检出所需的提交
cd libs/lib
git checkout <desired-commit>
cd ..
git add libs/lib
git commit -m "fix: resolve submodule conflict"
# 使用 SSH URL
git config -f .gitmodules submodule.libs/lib.url git@github.com:org/private-lib.git
git submodule sync
git submodule update --init
# 检查子模块内的更改
cd libs/lib
git status
git diff
# 丢弃更改
git checkout .
git clean -fd
# 或者提交
git add .
git commit -m "fix: resolve changes"
git push
# 在 diff 中显示子模块更改
git config --global diff.submodule log
# 在 status 中显示子模块摘要
git config --global status.submoduleSummary true
# 推送时检查子模块更改
git config --global push.recurseSubmodules check
# 获取时也获取子模块
git config --global fetch.recurseSubmodules on-demand
[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
每周安装数
10.4K
仓库
GitHub 星标数
88
首次出现
2026 年 1 月 24 日
安全审计
安装于
codex10.3K
gemini-cli10.3K
opencode10.3K
github-copilot10.3K
cursor10.3K
amp10.3K
Git submodule is a feature for including other Git repositories within a main Git repository.
Key concepts :
.gitmodules fileBasic 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"
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
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
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
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'
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
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
# 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
# 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'
# 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
# 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"
# 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
]
# Initialize all nested submodules
git submodule update --init --recursive
# Update all nested submodules
git submodule update --remote --recursive
# 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
# 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"
# 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
--recursive option in CI/CD pipelines--depth option for large repositories to save spacegit submodule status before committinggit submodule update --init is required after cloning.gitmodules can cause issues in forks.git/modules cache when removing a submodule# Force initialize
git submodule update --init --force
# 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"
# 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
# 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
# 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
[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
Weekly Installs
10.4K
Repository
GitHub Stars
88
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex10.3K
gemini-cli10.3K
opencode10.3K
github-copilot10.3K
cursor10.3K
amp10.3K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
122,000 周安装
小红书数据分析平台 Xinhong Data:账户增长、内容表现、达人合作、行业趋势深度洞察
62 周安装
KeyID Agent Kit MCP:为AI代理提供免费电子邮件地址和27种工具
570 周安装
Medusa 管理员用户创建指南:使用 new-user Skill 快速配置后台账户
562 周安装
数据分析验证工具 - 分享前审查分析准确性、方法论和潜在偏见
581 周安装
UI/UX设计师专家技能:精通设计系统、无障碍设计与现代工作流程
569 周安装
Grimmory 自托管图书馆管理器:支持 EPUB/PDF/MOBI/CBZ 的 Docker 部署方案
588 周安装