175 lines
3.8 KiB
Markdown
175 lines
3.8 KiB
Markdown
# 安装 Gerrit commit-msg Hook 指南
|
||
|
||
## 问题
|
||
|
||
推送代码时出现错误:
|
||
```
|
||
remote: ERROR: commit 5ee73b7: missing Change-Id in message footer
|
||
```
|
||
|
||
## 原因
|
||
|
||
Gerrit 要求每个提交都必须包含 `Change-Id`,用于跟踪代码变更。需要安装 `commit-msg` hook 来自动添加。
|
||
|
||
## 解决方案
|
||
|
||
### 方法一:使用 SCP 下载(SSH 方式,推荐)
|
||
|
||
在您的项目目录中执行:
|
||
|
||
```bash
|
||
cd /d/zhini_im_android
|
||
|
||
# 下载 commit-msg hook
|
||
gitdir=$(git rev-parse --git-dir)
|
||
scp -p -P 29419 renjianbo@101.43.95.130:hooks/commit-msg ${gitdir}/hooks/
|
||
|
||
# 如果使用 OpenSSH >= 9.0,需要添加 -O 参数
|
||
# scp -O -p -P 29419 renjianbo@101.43.95.130:hooks/commit-msg ${gitdir}/hooks/
|
||
|
||
# 设置执行权限
|
||
chmod +x ${gitdir}/hooks/commit-msg
|
||
```
|
||
|
||
### 方法二:使用 HTTP 下载(如果 SSH 有问题)
|
||
|
||
```bash
|
||
cd /d/zhini_im_android
|
||
|
||
# 下载 commit-msg hook
|
||
f="$(git rev-parse --git-dir)/hooks/commit-msg"
|
||
curl -o "$f" http://101.43.95.130:8080/tools/hooks/commit-msg
|
||
chmod +x "$f"
|
||
```
|
||
|
||
### 方法三:手动创建目录并下载
|
||
|
||
```bash
|
||
cd /d/zhini_im_android
|
||
|
||
# 确保 hooks 目录存在
|
||
mkdir -p .git/hooks
|
||
|
||
# 下载 hook(HTTP 方式)
|
||
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
||
|
||
# 设置执行权限
|
||
chmod +x .git/hooks/commit-msg
|
||
```
|
||
|
||
## 修复已提交的代码
|
||
|
||
安装 hook 后,需要修改最后一次提交以添加 Change-Id:
|
||
|
||
```bash
|
||
cd /d/zhini_im_android
|
||
|
||
# 修改最后一次提交(会自动添加 Change-Id)
|
||
git commit --amend --no-edit
|
||
|
||
# 重新推送
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
## 完整操作步骤
|
||
|
||
### 步骤 1:安装 commit-msg hook
|
||
|
||
```bash
|
||
cd /d/zhini_im_android
|
||
|
||
# 使用 HTTP 方式下载(最简单)
|
||
f="$(git rev-parse --git-dir)/hooks/commit-msg"
|
||
curl -o "$f" http://101.43.95.130:8080/tools/hooks/commit-msg
|
||
chmod +x "$f"
|
||
```
|
||
|
||
### 步骤 2:验证 hook 已安装
|
||
|
||
```bash
|
||
# 检查文件是否存在
|
||
ls -la .git/hooks/commit-msg
|
||
|
||
# 检查文件内容(应该看到 Gerrit commit-msg hook 的代码)
|
||
head -20 .git/hooks/commit-msg
|
||
```
|
||
|
||
### 步骤 3:修改提交添加 Change-Id
|
||
|
||
```bash
|
||
# 修改最后一次提交
|
||
git commit --amend --no-edit
|
||
|
||
# 查看提交信息,确认 Change-Id 已添加
|
||
git log -1
|
||
```
|
||
|
||
您应该看到提交信息末尾有类似这样的内容:
|
||
```
|
||
Change-Id: I1234567890abcdef1234567890abcdef12345678
|
||
```
|
||
|
||
### 步骤 4:重新推送
|
||
|
||
```bash
|
||
git push origin HEAD:refs/for/master
|
||
```
|
||
|
||
## 验证
|
||
|
||
推送成功后,您会看到类似这样的输出:
|
||
|
||
```
|
||
remote: Resolving deltas: 100% (55/55)
|
||
remote: Processing changes: refs: 1, new: 1
|
||
remote:
|
||
remote: SUCCESS
|
||
remote:
|
||
remote: http://101.43.95.130:8080/c/zhini_im_android/+/123 [NEW]
|
||
remote:
|
||
To ssh://101.43.95.130:29419/zhini_im_android
|
||
* [new branch] HEAD -> refs/for/master
|
||
```
|
||
|
||
## 后续使用
|
||
|
||
安装 hook 后,以后的所有新提交都会自动包含 Change-Id,不需要手动添加。
|
||
|
||
## 常见问题
|
||
|
||
### Q: 如果 scp 命令失败怎么办?
|
||
|
||
**A:** 使用 HTTP 方式下载:
|
||
```bash
|
||
curl -o .git/hooks/commit-msg http://101.43.95.130:8080/tools/hooks/commit-msg
|
||
chmod +x .git/hooks/commit-msg
|
||
```
|
||
|
||
### Q: 如果有多个提交需要修复怎么办?
|
||
|
||
**A:** 可以使用交互式 rebase:
|
||
```bash
|
||
# 假设需要修复最近 3 个提交
|
||
git rebase -i HEAD~3
|
||
|
||
# 在编辑器中,将要修改的提交标记为 "edit"
|
||
# 然后对每个提交执行:
|
||
git commit --amend --no-edit
|
||
git rebase --continue
|
||
```
|
||
|
||
### Q: hook 安装后不工作?
|
||
|
||
**A:** 检查:
|
||
1. 文件权限:`chmod +x .git/hooks/commit-msg`
|
||
2. 文件路径:确保在 `.git/hooks/` 目录下
|
||
3. Git 版本:确保 Git 版本支持 hooks
|
||
|
||
## 注意事项
|
||
|
||
- ✅ Hook 只需要安装一次,后续提交会自动使用
|
||
- ✅ 如果克隆新项目,需要重新安装 hook
|
||
- ✅ Hook 是项目级别的,每个项目都需要单独安装
|
||
|
||
|