Skip to content

Commit 45259a1

Browse files
committed
docs: add devops-submodule skill
1 parent adbe99f commit 45259a1

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
name: devops-submodule
3+
description: Git 子模块操作技能,用于管理主仓库的子模块,包括更新、提交、推送等操作。
4+
---
5+
6+
# Git 子模块操作技能
7+
8+
## 查看子模块状态
9+
10+
```bash
11+
git submodule status
12+
```
13+
14+
## 更新子模块
15+
16+
```bash
17+
# 初始化并更新所有子模块
18+
git submodule update --init --recursive
19+
20+
# 更新特定子模块
21+
git submodule update --init <子模块路径>
22+
```
23+
24+
## 子模块内操作
25+
26+
### 在子模块中提交
27+
28+
```bash
29+
# 进入子模块目录
30+
cd docs/handbook
31+
32+
# 检查状态
33+
git status
34+
35+
# 添加并提交
36+
git add -A
37+
git commit -m "docs: 更新文档"
38+
39+
# 推送到远程
40+
git push
41+
```
42+
43+
### 在子模块中创建分支
44+
45+
```bash
46+
cd docs/handbook
47+
git checkout -b feature/my-feature
48+
```
49+
50+
## 主仓库更新子模块引用
51+
52+
```bash
53+
# 返回主仓库
54+
cd ../..
55+
56+
# 添加子模块引用变更
57+
git add docs/handbook
58+
59+
# 提交子模块引用更新
60+
git commit -m "chore: update handbook submodule"
61+
62+
# 推送到远程
63+
git push
64+
```
65+
66+
## 添加新子模块
67+
68+
```bash
69+
# 添加子模块
70+
git submodule add <仓库URL> <子模块路径>
71+
72+
# 初始化子模块
73+
git submodule init
74+
```
75+
76+
## 删除子模块
77+
78+
```bash
79+
# 取消注册子模块
80+
git submodule deinit -f <子模块路径>
81+
82+
# 删除子模块目录
83+
rm -rf <子模块路径>
84+
85+
# 从版本控制中删除
86+
git rm -f <子模块路径>
87+
88+
# 删除 .git/modules 中的缓存
89+
rm -rf .git/modules/<子模块路径>
90+
```
91+
92+
## 常见场景
93+
94+
### 场景一:更新现有子模块内容
95+
96+
```bash
97+
# 1. 在子模块中提交并推送
98+
cd docs/gallery
99+
git add -A
100+
git commit -m "docs: 添加新示例"
101+
git push
102+
103+
# 2. 返回主仓库,更新引用
104+
cd ../..
105+
git add docs/gallery
106+
git commit -m "chore: update gallery submodule"
107+
git push
108+
```
109+
110+
### 场景二:切换子模块分支
111+
112+
```bash
113+
cd docs/handbook
114+
git checkout main
115+
git pull origin main
116+
cd ..
117+
git add docs/handbook
118+
git commit -m "chore: switch handbook to main branch"
119+
```
120+
121+
### 场景三:子模块回滚
122+
123+
```bash
124+
cd docs/handbook
125+
git log --oneline -5
126+
git reset --hard <commit-hash>
127+
git push --force
128+
cd ..
129+
git add docs/handbook
130+
git commit -m "chore: rollback handbook submodule"
131+
```

0 commit comments

Comments
 (0)