@zjex/git-workflow 0.4.2 → 0.4.3
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/CHANGELOG.md +11 -0
- package/README.md +1 -1
- package/dist/index.js +323 -11
- package/docs/.vitepress/config.ts +2 -0
- package/docs/commands/amend-date.md +425 -0
- package/docs/commands/amend.md +380 -0
- package/docs/commands/index.md +14 -10
- package/package.json +1 -1
- package/src/commands/amend-date.ts +228 -0
- package/src/commands/amend.ts +189 -0
- package/src/index.ts +39 -4
- package/tests/amend-date.test.ts +364 -0
- package/tests/amend.test.ts +441 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
# 修改提交时间
|
|
2
|
+
|
|
3
|
+
修改指定 commit 的提交时间(Author Date 和 Committer Date)。
|
|
4
|
+
|
|
5
|
+
## 命令
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 交互式选择 commit
|
|
9
|
+
gw amend:date
|
|
10
|
+
gw ad
|
|
11
|
+
|
|
12
|
+
# 直接指定 commit hash
|
|
13
|
+
gw ad a1b2c3d
|
|
14
|
+
gw ad HEAD~2
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 使用方式
|
|
18
|
+
|
|
19
|
+
### 方式 1: 交互式选择(推荐)
|
|
20
|
+
|
|
21
|
+
不带参数运行,从最近 20 个 commit 中选择:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gw ad
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 方式 2: 指定 commit hash
|
|
28
|
+
|
|
29
|
+
直接指定要修改的 commit:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# 使用短 hash
|
|
33
|
+
gw ad a1b2c3d
|
|
34
|
+
|
|
35
|
+
# 使用完整 hash
|
|
36
|
+
gw ad a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
|
|
37
|
+
|
|
38
|
+
# 使用相对引用
|
|
39
|
+
gw ad HEAD # 最新 commit
|
|
40
|
+
gw ad HEAD~1 # 上一个 commit
|
|
41
|
+
gw ad HEAD~2 # 上上个 commit
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 交互流程
|
|
45
|
+
|
|
46
|
+
### 步骤 1: 选择 commit(如果未指定)
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
? 选择要修改时间的 commit:
|
|
50
|
+
❯ a1b2c3d ✨ feat(auth): 添加用户登录功能 2026-01-19 14:30:00 +0800
|
|
51
|
+
d4e5f6g 🐛 fix(auth): 修复登录bug 2026-01-18 10:20:00 +0800
|
|
52
|
+
h7i8j9k 📝 docs: 更新文档 2026-01-17 16:45:00 +0800
|
|
53
|
+
...
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 步骤 2: 显示当前信息
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
当前 commit 信息:
|
|
60
|
+
Hash: a1b2c3d
|
|
61
|
+
Message: ✨ feat(auth): 添加用户登录功能
|
|
62
|
+
Date: 2026-01-19 14:30:00 +0800
|
|
63
|
+
────────────────────────────────────────
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 步骤 3: 输入新日期
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
输入日期格式: YYYY-MM-DD (如: 2026-01-19)
|
|
70
|
+
|
|
71
|
+
? 输入新的日期: 2026-01-15
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**日期格式**: `YYYY-MM-DD`,时间默认为 `00:00:00`
|
|
75
|
+
|
|
76
|
+
### 步骤 4: 确认修改
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
修改预览:
|
|
80
|
+
Commit: a1b2c3d
|
|
81
|
+
旧时间: 2026-01-19 14:30:00 +0800
|
|
82
|
+
新时间: 2026-01-15 00:00:00
|
|
83
|
+
修改类型: Author + Committer (两者都修改)
|
|
84
|
+
────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
? 确认修改? (y/N)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 使用场景
|
|
90
|
+
|
|
91
|
+
### 场景 1: 修改最新 commit
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
gw ad HEAD
|
|
95
|
+
# 或
|
|
96
|
+
gw ad
|
|
97
|
+
|
|
98
|
+
# 输入日期: 2026-01-15
|
|
99
|
+
# 确认
|
|
100
|
+
|
|
101
|
+
✔ 修改成功
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**特点**: 使用 `git commit --amend`,不会改变 commit hash
|
|
105
|
+
|
|
106
|
+
### 场景 2: 修改历史 commit
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
gw ad HEAD~3
|
|
110
|
+
|
|
111
|
+
# 输入日期: 2026-01-10
|
|
112
|
+
# 确认
|
|
113
|
+
|
|
114
|
+
⚠️ 警告: 修改非最新 commit 需要使用 rebase,可能会改变 commit hash
|
|
115
|
+
这会影响已推送到远程的 commit,请谨慎操作
|
|
116
|
+
|
|
117
|
+
? 确认修改? (y/N) y
|
|
118
|
+
|
|
119
|
+
正在执行 rebase...
|
|
120
|
+
✔ 修改成功
|
|
121
|
+
|
|
122
|
+
⚠️ 注意: commit hash 已改变
|
|
123
|
+
如果已推送到远程,需要使用 force push:
|
|
124
|
+
git push --force-with-lease
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**特点**: 使用 `git filter-branch`,会改变 commit hash
|
|
128
|
+
|
|
129
|
+
### 场景 3: 批量修改多个 commit
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# 修改第一个
|
|
133
|
+
gw ad HEAD~2
|
|
134
|
+
# 输入: 2026-01-10
|
|
135
|
+
|
|
136
|
+
# 修改第二个
|
|
137
|
+
gw ad HEAD~1
|
|
138
|
+
# 输入: 2026-01-11
|
|
139
|
+
|
|
140
|
+
# 修改最新的
|
|
141
|
+
gw ad HEAD
|
|
142
|
+
# 输入: 2026-01-12
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## 注意事项
|
|
146
|
+
|
|
147
|
+
### ⚠️ 修改历史 commit
|
|
148
|
+
|
|
149
|
+
- **会改变 commit hash** - 所有后续 commit 的 hash 都会改变
|
|
150
|
+
- **影响团队协作** - 如果已推送到远程,其他人需要重新拉取
|
|
151
|
+
- **需要 force push** - 推送时必须使用 `--force-with-lease`
|
|
152
|
+
|
|
153
|
+
### � Fo前rce Push
|
|
154
|
+
|
|
155
|
+
修改已推送的 commit 后,需要强制推送:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# 推荐:更安全的 force push
|
|
159
|
+
git push --force-with-lease
|
|
160
|
+
|
|
161
|
+
# 或:强制推送(不推荐)
|
|
162
|
+
git push --force
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### 📅 时间格式
|
|
166
|
+
|
|
167
|
+
- **输入格式**: `YYYY-MM-DD`(如 `2026-01-19`)
|
|
168
|
+
- **存储格式**: `YYYY-MM-DD 00:00:00`(时间默认为 00:00:00)
|
|
169
|
+
- **时区**: 使用系统当前时区
|
|
170
|
+
|
|
171
|
+
### 🔄 修改范围
|
|
172
|
+
|
|
173
|
+
默认同时修改:
|
|
174
|
+
|
|
175
|
+
- **Author Date**: 代码编写时间
|
|
176
|
+
- **Committer Date**: 代码提交时间
|
|
177
|
+
|
|
178
|
+
## 示例
|
|
179
|
+
|
|
180
|
+
### 示例 1: 修改最新 commit 为指定日期
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
$ gw ad HEAD
|
|
184
|
+
输入新的日期: 2026-01-15
|
|
185
|
+
确认修改? y
|
|
186
|
+
|
|
187
|
+
✔ 修改成功
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### 示例 2: 修改指定 hash 的 commit
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
$ gw ad a1b2c3d
|
|
194
|
+
输入新的日期: 2026-01-10
|
|
195
|
+
确认修改? y
|
|
196
|
+
|
|
197
|
+
⚠️ 警告: 修改非最新 commit 需要使用 rebase,可能会改变 commit hash
|
|
198
|
+
正在执行 rebase...
|
|
199
|
+
✔ 修改成功
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 示例 3: 交互式选择并修改
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
$ gw ad
|
|
206
|
+
|
|
207
|
+
? 选择要修改时间的 commit:
|
|
208
|
+
❯ a1b2c3d ✨ feat: 添加登录功能 2026-01-19 14:30:00
|
|
209
|
+
d4e5f6g 🐛 fix: 修复bug 2026-01-18 10:20:00
|
|
210
|
+
|
|
211
|
+
输入新的日期: 2026-01-15
|
|
212
|
+
确认修改? y
|
|
213
|
+
|
|
214
|
+
✔ 修改成功
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## 相关命令
|
|
218
|
+
|
|
219
|
+
- `gw c` - 提交代码
|
|
220
|
+
- `gw log` - 查看提交日志
|
|
221
|
+
- `git log --format="%ai"` - 查看 Author Date
|
|
222
|
+
- `git log --format="%ci"` - 查看 Committer Date
|
|
223
|
+
|
|
224
|
+
## 技术细节
|
|
225
|
+
|
|
226
|
+
### Author Date vs Committer Date
|
|
227
|
+
|
|
228
|
+
Git 中每个 commit 都有两个时间戳:
|
|
229
|
+
|
|
230
|
+
#### 📝 Author Date (作者时间)
|
|
231
|
+
|
|
232
|
+
- **含义**: 代码实际编写的时间
|
|
233
|
+
- **设置时机**: 第一次创建 commit 时
|
|
234
|
+
- **保持不变**: 即使 rebase、cherry-pick 也会保留原始作者时间
|
|
235
|
+
- **查看命令**: `git log --format="%ai"`
|
|
236
|
+
|
|
237
|
+
**示例场景**:
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# 2026-01-15 你写了代码并提交
|
|
241
|
+
git commit -m "feat: 添加登录功能"
|
|
242
|
+
# Author Date: 2026-01-15 10:00:00
|
|
243
|
+
|
|
244
|
+
# 2026-01-20 你 rebase 了这个 commit
|
|
245
|
+
git rebase -i HEAD~5
|
|
246
|
+
# Author Date: 仍然是 2026-01-15 10:00:00 (不变)
|
|
247
|
+
# Committer Date: 2026-01-20 14:30:00 (更新为 rebase 时间)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### 📦 Committer Date (提交时间)
|
|
251
|
+
|
|
252
|
+
- **含义**: 代码最后一次被提交到仓库的时间
|
|
253
|
+
- **设置时机**: 每次 commit、amend、rebase、cherry-pick 时
|
|
254
|
+
- **会更新**: 任何修改 commit 的操作都会更新这个时间
|
|
255
|
+
- **查看命令**: `git log --format="%ci"`
|
|
256
|
+
|
|
257
|
+
**示例场景**:
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
# 原始提交
|
|
261
|
+
git commit -m "feat: 添加登录功能"
|
|
262
|
+
# Author Date: 2026-01-15 10:00:00
|
|
263
|
+
# Committer Date: 2026-01-15 10:00:00
|
|
264
|
+
|
|
265
|
+
# 修改 commit message
|
|
266
|
+
git commit --amend -m "feat(auth): 添加用户登录功能"
|
|
267
|
+
# Author Date: 2026-01-15 10:00:00 (不变)
|
|
268
|
+
# Committer Date: 2026-01-15 10:30:00 (更新)
|
|
269
|
+
|
|
270
|
+
# 再次修改
|
|
271
|
+
git commit --amend --no-edit
|
|
272
|
+
# Author Date: 2026-01-15 10:00:00 (不变)
|
|
273
|
+
# Committer Date: 2026-01-15 11:00:00 (再次更新)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### 🔄 两者的关系
|
|
277
|
+
|
|
278
|
+
| 操作 | Author Date | Committer Date |
|
|
279
|
+
| -------------------- | ------------------ | ------------------ |
|
|
280
|
+
| `git commit` | 设置为当前时间 | 设置为当前时间 |
|
|
281
|
+
| `git commit --amend` | 保持不变 | 更新为当前时间 |
|
|
282
|
+
| `git rebase` | 保持不变 | 更新为当前时间 |
|
|
283
|
+
| `git cherry-pick` | 保持原 commit 时间 | 设置为当前时间 |
|
|
284
|
+
| `git merge` | 保持原 commit 时间 | 保持原 commit 时间 |
|
|
285
|
+
|
|
286
|
+
### 🎯 为什么要同时修改两者?
|
|
287
|
+
|
|
288
|
+
在大多数情况下,我们希望这两个时间保持一致,因为:
|
|
289
|
+
|
|
290
|
+
1. **简化理解**: 避免混淆,一个 commit 只有一个时间
|
|
291
|
+
2. **历史清晰**: 提交历史更容易理解和追溯
|
|
292
|
+
3. **避免误导**: 防止出现"作者时间是昨天,但提交时间是今天"的困惑
|
|
293
|
+
|
|
294
|
+
**本命令默认同时修改两者**,确保时间一致性。
|
|
295
|
+
|
|
296
|
+
### 📊 查看两个时间
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
# 查看 Author Date
|
|
300
|
+
git log --format="%ai %s"
|
|
301
|
+
# 输出: 2026-01-15 10:00:00 +0800 feat: 添加登录功能
|
|
302
|
+
|
|
303
|
+
# 查看 Committer Date
|
|
304
|
+
git log --format="%ci %s"
|
|
305
|
+
# 输出: 2026-01-15 10:00:00 +0800 feat: 添加登录功能
|
|
306
|
+
|
|
307
|
+
# 同时查看两者
|
|
308
|
+
git log --format="Author: %ai | Committer: %ci | %s"
|
|
309
|
+
# 输出: Author: 2026-01-15 10:00:00 +0800 | Committer: 2026-01-15 10:00:00 +0800 | feat: 添加登录功能
|
|
310
|
+
|
|
311
|
+
# 使用 gw log 查看(更友好)
|
|
312
|
+
gw log
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
### 实现方式
|
|
316
|
+
|
|
317
|
+
**最新 commit**:
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
GIT_AUTHOR_DATE="2026-01-15 00:00:00" \
|
|
321
|
+
GIT_COMMITTER_DATE="2026-01-15 00:00:00" \
|
|
322
|
+
git commit --amend --no-edit
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
**历史 commit**:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
git filter-branch -f --env-filter '
|
|
329
|
+
if [ "$GIT_COMMIT" = "commit-hash" ]; then
|
|
330
|
+
export GIT_AUTHOR_DATE="2026-01-15 00:00:00"
|
|
331
|
+
export GIT_COMMITTER_DATE="2026-01-15 00:00:00"
|
|
332
|
+
fi
|
|
333
|
+
' parent-hash..HEAD
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## 常见问题
|
|
337
|
+
|
|
338
|
+
### Q: 为什么 Git 要有两个时间?
|
|
339
|
+
|
|
340
|
+
A: 这是为了区分"谁写的代码"和"谁提交的代码"。在团队协作中很有用:
|
|
341
|
+
|
|
342
|
+
**场景 1: 代码审查**
|
|
343
|
+
|
|
344
|
+
```bash
|
|
345
|
+
# 小明写了代码
|
|
346
|
+
git commit -m "feat: 添加功能"
|
|
347
|
+
# Author: 小明, Author Date: 2026-01-15
|
|
348
|
+
|
|
349
|
+
# 小红审查后合并(使用 cherry-pick)
|
|
350
|
+
git cherry-pick abc123
|
|
351
|
+
# Author: 小明, Author Date: 2026-01-15 (保留原作者)
|
|
352
|
+
# Committer: 小红, Committer Date: 2026-01-16 (记录合并者)
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
**场景 2: 补丁应用**
|
|
356
|
+
|
|
357
|
+
```bash
|
|
358
|
+
# 开源项目收到贡献者的补丁
|
|
359
|
+
git am < patch.diff
|
|
360
|
+
# Author: 贡献者, Author Date: 补丁创建时间
|
|
361
|
+
# Committer: 维护者, Committer Date: 应用补丁时间
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Q: 为什么时间是 00:00:00?
|
|
365
|
+
|
|
366
|
+
A: 为了简化操作,只需要输入日期(YYYY-MM-DD),时间默认为 00:00:00。大多数情况下,我们只关心日期,不需要精确到秒。
|
|
367
|
+
|
|
368
|
+
### Q: 可以修改时间部分吗?
|
|
369
|
+
|
|
370
|
+
A: 当前版本只支持修改日期,时间固定为 00:00:00。如需精确时间,可以手动使用 git 命令:
|
|
371
|
+
|
|
372
|
+
```bash
|
|
373
|
+
# 修改最新 commit 的时间为指定的精确时间
|
|
374
|
+
GIT_AUTHOR_DATE="2026-01-15 14:30:00" \
|
|
375
|
+
GIT_COMMITTER_DATE="2026-01-15 14:30:00" \
|
|
376
|
+
git commit --amend --no-edit
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Q: 什么时候两个时间会不一致?
|
|
380
|
+
|
|
381
|
+
A: 以下操作会导致两个时间不一致:
|
|
382
|
+
|
|
383
|
+
1. **修改 commit**: `git commit --amend` 会更新 Committer Date
|
|
384
|
+
2. **Rebase**: `git rebase` 会更新所有 commit 的 Committer Date
|
|
385
|
+
3. **Cherry-pick**: `git cherry-pick` 会保留 Author Date,但设置新的 Committer Date
|
|
386
|
+
4. **手动修改**: 使用环境变量单独修改某个时间
|
|
387
|
+
|
|
388
|
+
**示例**:
|
|
389
|
+
|
|
390
|
+
```bash
|
|
391
|
+
# 原始 commit
|
|
392
|
+
git commit -m "feat: 功能"
|
|
393
|
+
# Author Date: 2026-01-15 10:00:00
|
|
394
|
+
# Committer Date: 2026-01-15 10:00:00
|
|
395
|
+
|
|
396
|
+
# 修改 commit message
|
|
397
|
+
git commit --amend -m "feat: 新功能"
|
|
398
|
+
# Author Date: 2026-01-15 10:00:00 (不变)
|
|
399
|
+
# Committer Date: 2026-01-15 11:00:00 (更新)
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Q: 为什么本命令要同时修改两者?
|
|
403
|
+
|
|
404
|
+
A: 保持时间一致性,避免混淆。如果你需要单独修改某个时间,可以使用原生 git 命令:
|
|
405
|
+
|
|
406
|
+
```bash
|
|
407
|
+
# 只修改 Author Date
|
|
408
|
+
GIT_AUTHOR_DATE="2026-01-15 00:00:00" git commit --amend --no-edit
|
|
409
|
+
|
|
410
|
+
# 只修改 Committer Date
|
|
411
|
+
GIT_COMMITTER_DATE="2026-01-15 00:00:00" git commit --amend --no-edit
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
### Q: 修改后如何验证?
|
|
415
|
+
|
|
416
|
+
A: 使用 `gw log` 或 `git log` 查看提交历史,确认时间已更新。
|
|
417
|
+
|
|
418
|
+
### Q: 修改失败怎么办?
|
|
419
|
+
|
|
420
|
+
A: 如果修改历史 commit 失败,可以使用 `git reflog` 找回之前的状态:
|
|
421
|
+
|
|
422
|
+
```bash
|
|
423
|
+
git reflog
|
|
424
|
+
git reset --hard HEAD@{1}
|
|
425
|
+
```
|