@zjex/git-workflow 0.2.24 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/deploy-docs.yml +68 -0
- package/.github/workflows/test.yml +24 -4
- package/.husky/pre-commit +14 -0
- package/README.md +72 -1066
- package/dist/index.js +103 -13
- package/docs/.vitepress/cache/deps/_metadata.json +52 -0
- package/docs/.vitepress/cache/deps/chunk-2CLQ7TTZ.js +9719 -0
- package/docs/.vitepress/cache/deps/chunk-2CLQ7TTZ.js.map +7 -0
- package/docs/.vitepress/cache/deps/chunk-LE5NDSFD.js +12824 -0
- package/docs/.vitepress/cache/deps/chunk-LE5NDSFD.js.map +7 -0
- package/docs/.vitepress/cache/deps/package.json +3 -0
- package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js +4505 -0
- package/docs/.vitepress/cache/deps/vitepress___@vue_devtools-api.js.map +7 -0
- package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js +583 -0
- package/docs/.vitepress/cache/deps/vitepress___@vueuse_core.js.map +7 -0
- package/docs/.vitepress/cache/deps/vitepress___@vueuse_integrations_useFocusTrap.js +1352 -0
- package/docs/.vitepress/cache/deps/vitepress___@vueuse_integrations_useFocusTrap.js.map +7 -0
- package/docs/.vitepress/cache/deps/vitepress___mark__js_src_vanilla__js.js +1665 -0
- package/docs/.vitepress/cache/deps/vitepress___mark__js_src_vanilla__js.js.map +7 -0
- package/docs/.vitepress/cache/deps/vitepress___minisearch.js +1813 -0
- package/docs/.vitepress/cache/deps/vitepress___minisearch.js.map +7 -0
- package/docs/.vitepress/cache/deps/vue.js +347 -0
- package/docs/.vitepress/cache/deps/vue.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/chunk-2CLQ7TTZ.js +9719 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/chunk-2CLQ7TTZ.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/chunk-LE5NDSFD.js +12824 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/chunk-LE5NDSFD.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/package.json +3 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vue_devtools-api.js +4505 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vue_devtools-api.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vueuse_core.js +583 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vueuse_core.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vueuse_integrations_useFocusTrap.js +1352 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___@vueuse_integrations_useFocusTrap.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___mark__js_src_vanilla__js.js +1665 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___mark__js_src_vanilla__js.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___minisearch.js +1813 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vitepress___minisearch.js.map +7 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vue.js +347 -0
- package/docs/.vitepress/cache/deps_temp_44e2fb0f/vue.js.map +7 -0
- package/docs/.vitepress/config.ts +167 -0
- package/docs/.vitepress/theme/custom.css +39 -0
- package/docs/.vitepress/theme/index.ts +4 -0
- package/docs/README.md +82 -0
- package/docs/commands/branch.md +468 -0
- package/docs/commands/commit.md +554 -0
- package/docs/commands/config.md +346 -0
- package/docs/commands/index.md +312 -0
- package/docs/commands/interactive.md +384 -0
- package/docs/commands/release.md +300 -0
- package/docs/commands/stash.md +309 -0
- package/docs/commands/tag.md +278 -0
- package/docs/commands/update.md +347 -0
- package/docs/config/ai-config.md +160 -0
- package/docs/config/branch-config.md +133 -0
- package/docs/config/commit-config.md +185 -0
- package/docs/config/config-file.md +776 -0
- package/docs/config/examples.md +279 -0
- package/docs/config/index.md +478 -0
- package/docs/guide/ai-commit.md +576 -0
- package/docs/guide/basic-usage.md +522 -0
- package/docs/guide/best-practices.md +426 -0
- package/docs/guide/branch-management.md +712 -0
- package/docs/guide/getting-started.md +294 -0
- package/docs/guide/index.md +168 -0
- package/docs/guide/installation.md +449 -0
- package/docs/guide/release-management.md +744 -0
- package/docs/guide/stash-management.md +608 -0
- package/docs/guide/tag-management.md +614 -0
- package/docs/index.md +205 -0
- package/docs/public/favicon.svg +21 -0
- package/docs/public/hero-logo.svg +43 -0
- package/docs/public/logo.svg +20 -0
- package/package.json +11 -2
- package/scripts/publish.js +55 -8
- package/scripts/publish.sh +20 -2
- package/scripts/release.sh +20 -2
- package/scripts/update-test-count.js +55 -0
- package/src/ai-service.ts +101 -15
- package/src/commands/init.ts +18 -0
- package/src/config.ts +1 -0
- package/tests/ai-service.test.ts +237 -2
- package/tests/help.test.ts +134 -0
- package/tests/init.test.ts +582 -0
- package/tests/release.test.ts +333 -0
- package/tests/setup.ts +21 -0
- package/tests/stash.test.ts +376 -0
- package/tests/update.test.ts +402 -0
- package/vitest.config.ts +3 -0
- package/zjex-logo.svg +22 -0
- package/zjex-optimized.svg +34 -0
- package/zjex.svg +1 -0
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
# 分支管理
|
|
2
|
+
|
|
3
|
+
分支管理是 Git Workflow 的核心功能,提供了规范化的分支创建、命名和生命周期管理。
|
|
4
|
+
|
|
5
|
+
## 🎯 分支管理理念
|
|
6
|
+
|
|
7
|
+
### 规范化命名
|
|
8
|
+
|
|
9
|
+
Git Workflow 自动生成规范的分支名称:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
feature/YYYYMMDD-[ID-]description
|
|
13
|
+
hotfix/YYYYMMDD-[ID-]description
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**优势:**
|
|
17
|
+
- 📅 **时间排序** - 按创建日期自然排序
|
|
18
|
+
- 🔍 **易于搜索** - 可以按日期或 ID 快速查找
|
|
19
|
+
- 📋 **可追溯性** - 每个分支都能追溯到具体需求
|
|
20
|
+
- 🤖 **自动生成** - 无需手动输入复杂的分支名
|
|
21
|
+
|
|
22
|
+
### 智能基础分支检测
|
|
23
|
+
|
|
24
|
+
自动检测合适的基础分支:
|
|
25
|
+
|
|
26
|
+
1. **配置文件设置** - 检查 `baseBranch` 配置
|
|
27
|
+
2. **自动检测** - 检测 `main` 或 `master` 分支
|
|
28
|
+
3. **当前分支** - 使用当前分支作为基础
|
|
29
|
+
|
|
30
|
+
## 🌿 Feature 分支管理
|
|
31
|
+
|
|
32
|
+
### 创建 Feature 分支
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
gw f
|
|
36
|
+
# 或使用别名
|
|
37
|
+
gw feature
|
|
38
|
+
gw feat
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 交互式创建流程
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
gw f
|
|
45
|
+
? 请输入 Story ID (可跳过): PROJ-123
|
|
46
|
+
? 请输入描述: add-user-authentication
|
|
47
|
+
✔ 分支创建成功: feature/20260111-PROJ-123-add-user-authentication
|
|
48
|
+
? 是否推送到远程? 是
|
|
49
|
+
✔ 已推送到远程: origin/feature/20260111-PROJ-123-add-user-authentication
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 高级选项
|
|
53
|
+
|
|
54
|
+
#### 指定基础分支
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# 基于 develop 分支创建
|
|
58
|
+
gw f --base develop
|
|
59
|
+
|
|
60
|
+
# 基于特定分支创建
|
|
61
|
+
gw f --base release/1.0
|
|
62
|
+
|
|
63
|
+
# 基于特定提交创建
|
|
64
|
+
git checkout abc1234
|
|
65
|
+
gw f
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### 处理未提交更改
|
|
69
|
+
|
|
70
|
+
当前分支有未提交更改时,Git Workflow 会智能处理:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
gw f
|
|
74
|
+
检测到未提交的更改:
|
|
75
|
+
M src/index.ts
|
|
76
|
+
M src/utils.ts
|
|
77
|
+
?? src/new-file.ts
|
|
78
|
+
────────────────────────────────────────
|
|
79
|
+
? 是否暂存 (stash) 这些更改后继续?
|
|
80
|
+
❯ 是,暂存更改并创建分支
|
|
81
|
+
否,取消操作
|
|
82
|
+
|
|
83
|
+
✔ 更改已暂存到 stash
|
|
84
|
+
✔ 分支创建成功: feature/20260111-PROJ-123-add-auth
|
|
85
|
+
💡 提示: 使用 'gw s' 可以恢复暂存的更改
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Feature 分支最佳实践
|
|
89
|
+
|
|
90
|
+
#### 1. 功能粒度控制
|
|
91
|
+
|
|
92
|
+
**推荐做法:**
|
|
93
|
+
```bash
|
|
94
|
+
# 单一功能分支
|
|
95
|
+
feature/20260111-PROJ-123-add-user-login
|
|
96
|
+
feature/20260111-PROJ-124-add-user-profile
|
|
97
|
+
feature/20260111-PROJ-125-add-user-settings
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**避免的做法:**
|
|
101
|
+
```bash
|
|
102
|
+
# 过于宽泛的分支
|
|
103
|
+
feature/20260111-PROJ-100-user-module
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### 2. 及时同步
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# 定期同步主分支
|
|
110
|
+
git checkout feature/20260111-PROJ-123-add-login
|
|
111
|
+
git pull origin develop
|
|
112
|
+
git merge develop
|
|
113
|
+
|
|
114
|
+
# 或使用 rebase
|
|
115
|
+
git rebase develop
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### 3. 保持分支整洁
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
# 开发过程中频繁提交
|
|
122
|
+
gw c # 每个逻辑单元都提交
|
|
123
|
+
|
|
124
|
+
# 合并前整理提交历史(可选)
|
|
125
|
+
git rebase -i develop
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 🐛 Hotfix 分支管理
|
|
129
|
+
|
|
130
|
+
### 创建 Hotfix 分支
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
gw h
|
|
134
|
+
# 或使用别名
|
|
135
|
+
gw hotfix
|
|
136
|
+
gw fix
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Hotfix 特点
|
|
140
|
+
|
|
141
|
+
**与 Feature 分支的区别:**
|
|
142
|
+
|
|
143
|
+
| 特性 | Feature 分支 | Hotfix 分支 |
|
|
144
|
+
| ---------- | ---------------------- | ---------------------- |
|
|
145
|
+
| 用途 | 新功能开发 | 紧急问题修复 |
|
|
146
|
+
| 基础分支 | develop(通常) | main/master(通常) |
|
|
147
|
+
| 生命周期 | 较长(数天到数周) | 较短(数小时到数天) |
|
|
148
|
+
| 合并策略 | 合并到 develop | 同时合并到 main 和 develop |
|
|
149
|
+
| ID 标签 | Story ID | Issue ID |
|
|
150
|
+
|
|
151
|
+
### Hotfix 工作流程
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# 1. 基于 main 创建 hotfix 分支
|
|
155
|
+
gw h --base main
|
|
156
|
+
# 输入: BUG-456, fix-payment-calculation
|
|
157
|
+
|
|
158
|
+
# 2. 快速修复问题
|
|
159
|
+
# 修改相关文件
|
|
160
|
+
|
|
161
|
+
# 3. 提交修复
|
|
162
|
+
gw c
|
|
163
|
+
# AI 生成: 🐛 fix(payment): 修复支付金额计算错误
|
|
164
|
+
|
|
165
|
+
# 4. 创建补丁版本
|
|
166
|
+
gw t
|
|
167
|
+
# 选择: patch → v1.2.1
|
|
168
|
+
|
|
169
|
+
# 5. 合并到 main 分支
|
|
170
|
+
git checkout main
|
|
171
|
+
git merge hotfix/20260111-BUG-456-fix-payment-calculation
|
|
172
|
+
|
|
173
|
+
# 6. 合并到 develop 分支
|
|
174
|
+
git checkout develop
|
|
175
|
+
git merge hotfix/20260111-BUG-456-fix-payment-calculation
|
|
176
|
+
|
|
177
|
+
# 7. 删除 hotfix 分支
|
|
178
|
+
gw d
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 紧急修复场景
|
|
182
|
+
|
|
183
|
+
#### 1. 生产环境 Bug
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# 用户报告支付功能异常
|
|
187
|
+
gw h --base main
|
|
188
|
+
# BUG-789, fix-payment-gateway-timeout
|
|
189
|
+
|
|
190
|
+
# 快速定位和修复
|
|
191
|
+
# 提交修复
|
|
192
|
+
gw c
|
|
193
|
+
|
|
194
|
+
# 立即发布补丁版本
|
|
195
|
+
gw t # patch → v2.1.3
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### 2. 安全漏洞修复
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
# 发现安全漏洞
|
|
202
|
+
gw h --base main
|
|
203
|
+
# SEC-101, fix-sql-injection-vulnerability
|
|
204
|
+
|
|
205
|
+
# 修复安全问题
|
|
206
|
+
# 提交修复
|
|
207
|
+
gw c
|
|
208
|
+
|
|
209
|
+
# 发布安全补丁
|
|
210
|
+
gw t # patch → v2.1.4
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### 3. 关键功能故障
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
# 核心功能无法使用
|
|
217
|
+
gw h --base main
|
|
218
|
+
# CRIT-202, fix-database-connection-pool
|
|
219
|
+
|
|
220
|
+
# 修复关键问题
|
|
221
|
+
# 提交修复
|
|
222
|
+
gw c
|
|
223
|
+
|
|
224
|
+
# 发布紧急补丁
|
|
225
|
+
gw t # patch → v2.1.5
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## 🗑️ 分支清理管理
|
|
229
|
+
|
|
230
|
+
### 智能分支删除
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
gw d
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### 分支列表显示
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
? 选择要删除的分支:
|
|
240
|
+
❯ feature/20260105-PROJ-100-old-feature (本地+远程) 3 days ago
|
|
241
|
+
feature/20260103-PROJ-101-test-feature (仅本地) 5 days ago
|
|
242
|
+
hotfix/20260101-BUG-200-urgent-fix (本地+远程) 1 week ago
|
|
243
|
+
取消
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**信息说明:**
|
|
247
|
+
- **分支名** - 完整的分支名称
|
|
248
|
+
- **状态** - 本地分支、远程分支或两者都有
|
|
249
|
+
- **时间** - 最后提交时间,按最近使用排序
|
|
250
|
+
|
|
251
|
+
### 删除确认流程
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
分支信息:
|
|
255
|
+
名称: feature/20260105-PROJ-100-old-feature
|
|
256
|
+
最后提交: 3 days ago
|
|
257
|
+
提交信息: feat(user): 添加用户管理功能
|
|
258
|
+
状态: 本地+远程分支都存在
|
|
259
|
+
提交数: 15 commits ahead of develop
|
|
260
|
+
────────────────────────────────────────
|
|
261
|
+
? 确认删除?
|
|
262
|
+
❯ 是,删除本地和远程分支
|
|
263
|
+
仅删除本地分支
|
|
264
|
+
取消
|
|
265
|
+
|
|
266
|
+
✔ 本地分支已删除: feature/20260105-PROJ-100-old-feature
|
|
267
|
+
✔ 远程分支已删除: origin/feature/20260105-PROJ-100-old-feature
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### 批量清理策略
|
|
271
|
+
|
|
272
|
+
#### 1. 定期清理
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
# 每周清理一次已合并的分支
|
|
276
|
+
gw d
|
|
277
|
+
# 选择已合并的分支进行删除
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
#### 2. 按时间清理
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
# 删除超过一个月的分支
|
|
284
|
+
git for-each-ref --format='%(refname:short) %(committerdate)' refs/heads | \
|
|
285
|
+
awk '$2 < "'$(date -d '1 month ago' '+%Y-%m-%d')'"' | \
|
|
286
|
+
cut -d' ' -f1 | \
|
|
287
|
+
grep -E '^(feature|hotfix)/' | \
|
|
288
|
+
xargs -I {} git branch -D {}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
#### 3. 按状态清理
|
|
292
|
+
|
|
293
|
+
```bash
|
|
294
|
+
# 使用 Git 命令清理已合并的分支
|
|
295
|
+
git branch --merged develop | \
|
|
296
|
+
grep -E '^ (feature|hotfix)/' | \
|
|
297
|
+
xargs -I {} git branch -d {}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### 安全保护机制
|
|
301
|
+
|
|
302
|
+
**保护规则:**
|
|
303
|
+
- 🛡️ **当前分支保护** - 不能删除当前所在的分支
|
|
304
|
+
- 🛡️ **主分支保护** - 不显示 main/master/develop 等主分支
|
|
305
|
+
- 🛡️ **确认机制** - 删除前需要明确确认
|
|
306
|
+
- 🛡️ **状态检查** - 显示分支的本地/远程状态
|
|
307
|
+
|
|
308
|
+
## ⚙️ 分支配置管理
|
|
309
|
+
|
|
310
|
+
### 基础配置
|
|
311
|
+
|
|
312
|
+
```json
|
|
313
|
+
{
|
|
314
|
+
"baseBranch": "develop",
|
|
315
|
+
"featurePrefix": "feature",
|
|
316
|
+
"hotfixPrefix": "hotfix",
|
|
317
|
+
"requireId": true,
|
|
318
|
+
"featureIdLabel": "Story ID",
|
|
319
|
+
"hotfixIdLabel": "Issue ID",
|
|
320
|
+
"autoPush": true
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### 配置项详解
|
|
325
|
+
|
|
326
|
+
#### 分支前缀配置
|
|
327
|
+
|
|
328
|
+
```json
|
|
329
|
+
{
|
|
330
|
+
"featurePrefix": "feat",
|
|
331
|
+
"hotfixPrefix": "fix"
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
生成的分支名:
|
|
336
|
+
```
|
|
337
|
+
feat/20260111-PROJ-123-add-login
|
|
338
|
+
fix/20260111-BUG-456-fix-crash
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
#### ID 要求配置
|
|
342
|
+
|
|
343
|
+
```json
|
|
344
|
+
{
|
|
345
|
+
"requireId": true,
|
|
346
|
+
"featureIdLabel": "Jira ID",
|
|
347
|
+
"hotfixIdLabel": "Bug ID"
|
|
348
|
+
}
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**效果:**
|
|
352
|
+
- 创建分支时必须填写 ID,不能跳过
|
|
353
|
+
- 自定义 ID 输入提示文字
|
|
354
|
+
|
|
355
|
+
#### 自动推送配置
|
|
356
|
+
|
|
357
|
+
```json
|
|
358
|
+
{
|
|
359
|
+
"autoPush": true
|
|
360
|
+
}
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
**选项说明:**
|
|
364
|
+
- `true` - 创建分支后自动推送,不询问
|
|
365
|
+
- `false` - 创建分支后不推送,不询问
|
|
366
|
+
- 不设置 - 每次创建分支时询问(默认)
|
|
367
|
+
|
|
368
|
+
### 团队配置示例
|
|
369
|
+
|
|
370
|
+
#### 敏捷开发团队
|
|
371
|
+
|
|
372
|
+
```json
|
|
373
|
+
{
|
|
374
|
+
"baseBranch": "develop",
|
|
375
|
+
"requireId": true,
|
|
376
|
+
"featureIdLabel": "Story ID",
|
|
377
|
+
"hotfixIdLabel": "Bug ID",
|
|
378
|
+
"autoPush": true
|
|
379
|
+
}
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### 开源项目
|
|
383
|
+
|
|
384
|
+
```json
|
|
385
|
+
{
|
|
386
|
+
"baseBranch": "main",
|
|
387
|
+
"requireId": false,
|
|
388
|
+
"featurePrefix": "feature",
|
|
389
|
+
"hotfixPrefix": "hotfix",
|
|
390
|
+
"autoPush": false
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
#### 企业级项目
|
|
395
|
+
|
|
396
|
+
```json
|
|
397
|
+
{
|
|
398
|
+
"baseBranch": "develop",
|
|
399
|
+
"featurePrefix": "feat",
|
|
400
|
+
"hotfixPrefix": "fix",
|
|
401
|
+
"requireId": true,
|
|
402
|
+
"featureIdLabel": "需求编号",
|
|
403
|
+
"hotfixIdLabel": "缺陷编号",
|
|
404
|
+
"autoPush": true
|
|
405
|
+
}
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## 🔄 分支工作流模式
|
|
409
|
+
|
|
410
|
+
### Git Flow 模式
|
|
411
|
+
|
|
412
|
+
```mermaid
|
|
413
|
+
graph LR
|
|
414
|
+
A[main] --> B[develop]
|
|
415
|
+
B --> C[feature/xxx]
|
|
416
|
+
B --> D[release/xxx]
|
|
417
|
+
A --> E[hotfix/xxx]
|
|
418
|
+
C --> B
|
|
419
|
+
D --> A
|
|
420
|
+
D --> B
|
|
421
|
+
E --> A
|
|
422
|
+
E --> B
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
**配置:**
|
|
426
|
+
```json
|
|
427
|
+
{
|
|
428
|
+
"baseBranch": "develop",
|
|
429
|
+
"requireId": true,
|
|
430
|
+
"autoPush": true
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
**操作流程:**
|
|
435
|
+
```bash
|
|
436
|
+
# 功能开发
|
|
437
|
+
gw f --base develop
|
|
438
|
+
# 开发完成后合并到 develop
|
|
439
|
+
|
|
440
|
+
# 发布准备
|
|
441
|
+
git checkout -b release/1.2.0 develop
|
|
442
|
+
# 测试完成后合并到 main 和 develop
|
|
443
|
+
|
|
444
|
+
# 紧急修复
|
|
445
|
+
gw h --base main
|
|
446
|
+
# 修复完成后合并到 main 和 develop
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### GitHub Flow 模式
|
|
450
|
+
|
|
451
|
+
```mermaid
|
|
452
|
+
graph LR
|
|
453
|
+
A[main] --> B[feature/xxx]
|
|
454
|
+
B --> C[Pull Request]
|
|
455
|
+
C --> A
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
**配置:**
|
|
459
|
+
```json
|
|
460
|
+
{
|
|
461
|
+
"baseBranch": "main",
|
|
462
|
+
"requireId": false,
|
|
463
|
+
"autoPush": true
|
|
464
|
+
}
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
**操作流程:**
|
|
468
|
+
```bash
|
|
469
|
+
# 功能开发
|
|
470
|
+
gw f --base main
|
|
471
|
+
# 开发完成后创建 Pull Request
|
|
472
|
+
# 审查通过后合并到 main
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
### GitLab Flow 模式
|
|
476
|
+
|
|
477
|
+
```mermaid
|
|
478
|
+
graph LR
|
|
479
|
+
A[main] --> B[feature/xxx]
|
|
480
|
+
B --> C[Merge Request]
|
|
481
|
+
C --> A
|
|
482
|
+
A --> D[production]
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
**配置:**
|
|
486
|
+
```json
|
|
487
|
+
{
|
|
488
|
+
"baseBranch": "main",
|
|
489
|
+
"requireId": true,
|
|
490
|
+
"featureIdLabel": "Issue ID",
|
|
491
|
+
"autoPush": true
|
|
492
|
+
}
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
## 🎯 分支管理场景
|
|
496
|
+
|
|
497
|
+
### 场景一:并行功能开发
|
|
498
|
+
|
|
499
|
+
```bash
|
|
500
|
+
# 开发者 A
|
|
501
|
+
gw f --base develop
|
|
502
|
+
# PROJ-123, add-user-authentication
|
|
503
|
+
|
|
504
|
+
# 开发者 B
|
|
505
|
+
gw f --base develop
|
|
506
|
+
# PROJ-124, add-payment-system
|
|
507
|
+
|
|
508
|
+
# 开发者 C
|
|
509
|
+
gw f --base develop
|
|
510
|
+
# PROJ-125, add-notification-service
|
|
511
|
+
|
|
512
|
+
# 各自独立开发,完成后合并到 develop
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
### 场景二:功能依赖管理
|
|
516
|
+
|
|
517
|
+
```bash
|
|
518
|
+
# 基础功能分支
|
|
519
|
+
gw f --base develop
|
|
520
|
+
# PROJ-100, add-user-system
|
|
521
|
+
|
|
522
|
+
# 完成基础功能后,基于此分支创建依赖功能
|
|
523
|
+
git checkout feature/20260111-PROJ-100-add-user-system
|
|
524
|
+
gw f
|
|
525
|
+
# PROJ-101, add-user-profile
|
|
526
|
+
|
|
527
|
+
# 或者等基础功能合并后再开发
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
### 场景三:实验性功能
|
|
531
|
+
|
|
532
|
+
```bash
|
|
533
|
+
# 创建实验分支
|
|
534
|
+
gw f --base develop
|
|
535
|
+
# EXP-001, try-new-ui-framework
|
|
536
|
+
|
|
537
|
+
# 实验成功,继续开发
|
|
538
|
+
gw c # 提交实验结果
|
|
539
|
+
|
|
540
|
+
# 实验失败,直接删除分支
|
|
541
|
+
gw d # 删除实验分支
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### 场景四:长期功能分支
|
|
545
|
+
|
|
546
|
+
```bash
|
|
547
|
+
# 创建长期功能分支
|
|
548
|
+
gw f --base develop
|
|
549
|
+
# EPIC-001, redesign-user-interface
|
|
550
|
+
|
|
551
|
+
# 定期同步主分支
|
|
552
|
+
git pull origin develop
|
|
553
|
+
git merge develop
|
|
554
|
+
|
|
555
|
+
# 分阶段提交
|
|
556
|
+
gw c # 阶段一完成
|
|
557
|
+
gw c # 阶段二完成
|
|
558
|
+
gw c # 阶段三完成
|
|
559
|
+
|
|
560
|
+
# 功能完成后合并
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
## 🔧 高级分支技巧
|
|
564
|
+
|
|
565
|
+
### 技巧一:分支重命名
|
|
566
|
+
|
|
567
|
+
```bash
|
|
568
|
+
# 重命名当前分支
|
|
569
|
+
git branch -m new-branch-name
|
|
570
|
+
|
|
571
|
+
# 重命名其他分支
|
|
572
|
+
git branch -m old-name new-name
|
|
573
|
+
|
|
574
|
+
# 更新远程分支
|
|
575
|
+
git push origin :old-name new-name
|
|
576
|
+
git push origin -u new-name
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
### 技巧二:分支比较
|
|
580
|
+
|
|
581
|
+
```bash
|
|
582
|
+
# 比较分支差异
|
|
583
|
+
git diff develop..feature/20260111-PROJ-123-add-login
|
|
584
|
+
|
|
585
|
+
# 查看分支提交差异
|
|
586
|
+
git log develop..feature/20260111-PROJ-123-add-login --oneline
|
|
587
|
+
|
|
588
|
+
# 查看文件变更统计
|
|
589
|
+
git diff --stat develop..feature/20260111-PROJ-123-add-login
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### 技巧三:分支合并策略
|
|
593
|
+
|
|
594
|
+
```bash
|
|
595
|
+
# 快进合并(保持线性历史)
|
|
596
|
+
git merge --ff-only feature-branch
|
|
597
|
+
|
|
598
|
+
# 非快进合并(保留分支信息)
|
|
599
|
+
git merge --no-ff feature-branch
|
|
600
|
+
|
|
601
|
+
# 压缩合并(将多个提交合并为一个)
|
|
602
|
+
git merge --squash feature-branch
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
### 技巧四:分支恢复
|
|
606
|
+
|
|
607
|
+
```bash
|
|
608
|
+
# 查看删除的分支
|
|
609
|
+
git reflog
|
|
610
|
+
|
|
611
|
+
# 恢复误删的分支
|
|
612
|
+
git checkout -b recovered-branch <commit-hash>
|
|
613
|
+
|
|
614
|
+
# 或者
|
|
615
|
+
git branch recovered-branch <commit-hash>
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
## 🚨 常见问题解决
|
|
619
|
+
|
|
620
|
+
### 问题一:分支名冲突
|
|
621
|
+
|
|
622
|
+
```bash
|
|
623
|
+
❌ 分支 'feature/20260111-PROJ-123-add-login' 已存在
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
**解决方案:**
|
|
627
|
+
1. 使用不同的描述
|
|
628
|
+
2. 切换到现有分支继续开发
|
|
629
|
+
3. 删除现有分支后重新创建
|
|
630
|
+
|
|
631
|
+
### 问题二:基础分支不存在
|
|
632
|
+
|
|
633
|
+
```bash
|
|
634
|
+
❌ 基础分支 'develop' 不存在
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
**解决方案:**
|
|
638
|
+
```bash
|
|
639
|
+
# 创建并切换到 develop 分支
|
|
640
|
+
git checkout -b develop main
|
|
641
|
+
|
|
642
|
+
# 或从远程拉取
|
|
643
|
+
git fetch origin develop:develop
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
### 问题三:无法删除远程分支
|
|
647
|
+
|
|
648
|
+
```bash
|
|
649
|
+
❌ 远程分支删除失败: 权限不足
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
**解决方案:**
|
|
653
|
+
1. 检查推送权限
|
|
654
|
+
2. 手动删除:`git push origin --delete branch-name`
|
|
655
|
+
3. 联系仓库管理员
|
|
656
|
+
|
|
657
|
+
### 问题四:分支包含未合并的更改
|
|
658
|
+
|
|
659
|
+
```bash
|
|
660
|
+
❌ 分支包含未合并的更改,无法删除
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
**解决方案:**
|
|
664
|
+
```bash
|
|
665
|
+
# 强制删除本地分支
|
|
666
|
+
git branch -D branch-name
|
|
667
|
+
|
|
668
|
+
# 或先合并更改
|
|
669
|
+
git checkout main
|
|
670
|
+
git merge branch-name
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
## 📊 分支管理度量
|
|
674
|
+
|
|
675
|
+
### 关键指标
|
|
676
|
+
|
|
677
|
+
- **分支数量** - 当前活跃分支数量
|
|
678
|
+
- **分支生命周期** - 从创建到合并的平均时间
|
|
679
|
+
- **分支大小** - 每个分支的提交数量
|
|
680
|
+
- **合并频率** - 每天/周的分支合并次数
|
|
681
|
+
|
|
682
|
+
### 监控脚本
|
|
683
|
+
|
|
684
|
+
```bash
|
|
685
|
+
#!/bin/bash
|
|
686
|
+
# branch-metrics.sh
|
|
687
|
+
|
|
688
|
+
echo "=== 分支管理度量报告 ==="
|
|
689
|
+
echo "时间: $(date)"
|
|
690
|
+
echo
|
|
691
|
+
|
|
692
|
+
# 活跃分支统计
|
|
693
|
+
echo "📊 活跃分支统计:"
|
|
694
|
+
git branch -r | grep -E 'origin/(feature|hotfix)/' | wc -l | xargs echo "远程分支数:"
|
|
695
|
+
git branch | grep -E '^ (feature|hotfix)/' | wc -l | xargs echo "本地分支数:"
|
|
696
|
+
echo
|
|
697
|
+
|
|
698
|
+
# 分支年龄分析
|
|
699
|
+
echo "📊 分支年龄分析:"
|
|
700
|
+
git for-each-ref --format='%(refname:short) %(committerdate:relative)' refs/heads/ | \
|
|
701
|
+
grep -E '^(feature|hotfix)/' | \
|
|
702
|
+
head -10
|
|
703
|
+
echo
|
|
704
|
+
|
|
705
|
+
# 最近合并的分支
|
|
706
|
+
echo "📊 最近合并的分支:"
|
|
707
|
+
git log --merges --oneline --since="1 week ago" | head -5
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
---
|
|
711
|
+
|
|
712
|
+
通过系统化的分支管理,你可以建立清晰、高效的开发工作流。Git Workflow 的分支管理功能让复杂的 Git 操作变得简单,帮助团队保持代码库的整洁和可维护性。
|