推送到Gerrit时报错,缺少Change-Id
背景:Gerrit 是一个基于 Git 的 代码审查工具,可以更好地集成AI代码审核。
1、问题描述
代码提交,推送到Gerrit时报错
remote: Hint: to automatically insert a Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 12345 yonghai.xie@172.12.12.123:hooks/commit-msg ${gitdir}/hooks/
remote: (for OpenSSH >= 9.0 you need to add the flag '-O' to the scp command)
remote: or, for http(s):
remote: f="$(git rev-parse --git-dir)/hooks/commit-msg"; curl -o "$f" http://172.12.12.123:8080/tools/hooks/commit-msg ; chmod +x "$f"
remote: and then amend the commit:
remote: git commit --amend --no-edit
remote: Finally, push your changes again
remote:
error: failed to push some refs to 'http://172.12.12.123:8080/spring6'
2、问题分析
提交的 Git commit 缺少必要的 Change-Id 标识,导致远程仓库拒绝推送。
远程仓库规则限制:目标仓库使用了 Gerrit 或类似代码审查系统,要求每个提交必须包含 Change-Id。
未安装提交钩子:本地 Git 没有安装 Gerrit 提供的 commit-msg 钩子脚本,因此无法自动生成 Change-Id。
提交被拒绝:由于缺少 Change-Id,Gerrit 拒绝了此次 push 请求。
3、解决
依次执行以下命令
在对应项目的根目录文件夹下打开【Git Bash】
1.下载并安装 commit-msg 钩子
# 获取 commit-msg 钩子脚本
f="$(git rev-parse --git-dir)/hooks/commit-msg"
#172.12.12.123(自己具体的仓库地址)
curl -o "$f" http://172.12.12.123:8080/tools/hooks/commit-msg
# 添加可执行权限
chmod +x "$f"
2.修正最近一次提交以添加 Change-Id
# 修正最后一次提交,不修改提交信息内容
git commit --amend --no-edit
3.重新推送代码
# dev20250509(自己具体的分支)
git push origin HEAD:refs/for/dev20250509