|
| 1 | +# Submodule Commit Workflow Rule |
| 2 | + |
| 3 | +当在 Git Submodule 内做了改动时,提交前需要在每个有改动的 submodule 中分别切出新的分支,然后分别 push 每个 submodule。 |
| 4 | + |
| 5 | +## 触发条件 |
| 6 | + |
| 7 | +当准备提交代码时,检测到以下条件: |
| 8 | +1. 项目包含 Git Submodules(存在 `.gitmodules` 文件) |
| 9 | +2. 有一个或多个 submodule 内存在未提交的改动 |
| 10 | + |
| 11 | +## 检查 Submodule 状态 |
| 12 | + |
| 13 | +使用以下命令检查 submodule 是否有改动: |
| 14 | + |
| 15 | +```bash |
| 16 | +git submodule foreach 'git status --porcelain' |
| 17 | +``` |
| 18 | + |
| 19 | +如果输出不为空,说明对应的 submodule 有改动。 |
| 20 | + |
| 21 | +## 执行步骤 |
| 22 | + |
| 23 | +### 1. 识别有改动的 Submodules |
| 24 | + |
| 25 | +```bash |
| 26 | +git submodule foreach --quiet 'if [ -n "$(git status --porcelain)" ]; then echo $name; fi' |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. 对每个有改动的 Submodule 执行以下操作 |
| 30 | + |
| 31 | +#### 2.1 进入 Submodule 目录 |
| 32 | + |
| 33 | +```bash |
| 34 | +cd <submodule-path> |
| 35 | +``` |
| 36 | + |
| 37 | +#### 2.2 创建新分支 |
| 38 | + |
| 39 | +分支命名规则与主项目一致,参考 `auto-create-branch-on-master.md`: |
| 40 | + |
| 41 | +``` |
| 42 | +YYMMDD-(feat|fix|chore|refactor)-xxxxx-xxxx-xxxx |
| 43 | +``` |
| 44 | + |
| 45 | +示例: |
| 46 | +```bash |
| 47 | +git checkout -b 260204-feat-update-component |
| 48 | +``` |
| 49 | + |
| 50 | +#### 2.3 提交改动 |
| 51 | + |
| 52 | +```bash |
| 53 | +git add . |
| 54 | +git commit -m "<commit message>" |
| 55 | +``` |
| 56 | + |
| 57 | +#### 2.4 Push 到远程 |
| 58 | + |
| 59 | +```bash |
| 60 | +git push -u origin <branch-name> |
| 61 | +``` |
| 62 | + |
| 63 | +#### 2.5 返回主项目目录 |
| 64 | + |
| 65 | +```bash |
| 66 | +cd - |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. 更新主项目的 Submodule 引用 |
| 70 | + |
| 71 | +在所有 submodule 都提交并 push 后,回到主项目: |
| 72 | + |
| 73 | +```bash |
| 74 | +git add <submodule-path> |
| 75 | +git commit -m "chore: update submodule <submodule-name> reference" |
| 76 | +``` |
| 77 | + |
| 78 | +## 完整示例 |
| 79 | + |
| 80 | +假设项目有一个名为 `libs/common` 的 submodule,今天是 2026 年 2 月 4 日,修改了其中的组件代码: |
| 81 | + |
| 82 | +```bash |
| 83 | +# 1. 进入 submodule |
| 84 | +cd libs/common |
| 85 | + |
| 86 | +# 2. 创建新分支 |
| 87 | +git checkout -b 260204-feat-update-component |
| 88 | + |
| 89 | +# 3. 提交改动 |
| 90 | +git add . |
| 91 | +git commit -m "feat: update button component styles" |
| 92 | + |
| 93 | +# 4. Push 到远程 |
| 94 | +git push -u origin 260204-feat-update-component |
| 95 | + |
| 96 | +# 5. 返回主项目 |
| 97 | +cd - |
| 98 | + |
| 99 | +# 6. 更新主项目的 submodule 引用 |
| 100 | +git add libs/common |
| 101 | +git commit -m "chore: update submodule libs/common reference" |
| 102 | +``` |
| 103 | + |
| 104 | +## 多个 Submodule 的情况 |
| 105 | + |
| 106 | +如果有多个 submodule 都有改动,需要依次对每个 submodule 执行上述步骤: |
| 107 | + |
| 108 | +```bash |
| 109 | +# 列出所有有改动的 submodules |
| 110 | +git submodule foreach --quiet 'if [ -n "$(git status --porcelain)" ]; then echo $name; fi' |
| 111 | + |
| 112 | +# 对每个 submodule 分别执行:创建分支 → 提交 → push |
| 113 | +# 最后统一更新主项目的引用 |
| 114 | +``` |
| 115 | + |
| 116 | +## 注意事项 |
| 117 | + |
| 118 | +- 每个 submodule 的分支名应该与其改动内容相关,而非简单复制主项目的分支名 |
| 119 | +- 确保 submodule 的远程仓库有推送权限 |
| 120 | +- 如果 submodule 的远程仓库需要创建 Pull Request,请在 push 后创建对应的 PR |
| 121 | +- 主项目的 submodule 引用更新应该在所有 submodule 都成功 push 后再进行 |
| 122 | +- 如果某个 submodule push 失败,需要先解决问题后再继续 |
| 123 | + |
| 124 | +## 相关规则 |
| 125 | + |
| 126 | +- [auto-create-branch-on-master.md](./auto-create-branch-on-master.md) - 分支命名规范 |
| 127 | +- [git-submodule-check.md](./git-submodule-check.md) - Submodule 初始化检查 |
0 commit comments