一、问题:
在日常的工作中,使用git推送代码时会出现以下报错,“missing Change-Id in commit message” :
qinjiaxi:$ git push origin HEAD:refs/for/develop
对象计数中: , 完成.
Delta compression using up to threads.
压缩对象中: % (/), 完成.
写入对象中: % (/), 10.92 KiB | bytes/s, 完成.
Total (delta ), reused (delta )
remote: Resolving deltas: % (/)
remote: Processing changes: refs: , done
remote: ERROR: [abcdefg] missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P xxx.com.cn:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend
remote:
To ssh://xxx.com.cn:29418/ZXXXX-XXXX-XXXX
! [remote rejected] HEAD -> refs/for/develop ([abcdefg] missing Change-Id in commit message footer)
二、问题分析:
从上述错误信息中可以看出来,该提交缺少Change-ID。由于Change-ID对于git仓库来讲是该提交唯一可识别的ID,因此,缺少Change-ID是一个Fatal error,这样肯定是不能入库的。一般情况下,Change-ID都是由".git/hooks/commit-msg"
脚本在执行"git commit"
的时候自动生成的。如果出现上述错误,极有可能是当前git库下缺少上述".git/hooks/commit-msg"
脚本。(可能是克隆的时候没有选择Clone with commit-msg hook)
三、解决方案
3.1获取commit-msg脚本
使用提示中的命令获取commit-msg脚本:
gitdir=$(git rev-parse --git-dir); scp -p -P xxx.com.cn:hooks/commit-msg ${gitdir}/hooks/
注:会弹出一个页面,这里直接按照页面的提示退出就行
3.2追加提交:
git commit --amend
3.3推送到代码库:
git push origin HEAD:refs/for/develop
通常以上的几部能够解决问题,如果不能解决的话还可以用以下的方法
由于有多个commit这里我们需要merge合并这些commit为一个然后提交。(前提条件:使用git pull命令拉取最新代码)
1、首先我们创建一个分支
git branch develop_merge
2、然后我们切换到1中创建的分支并删除develop分支
git checkout develop_merge
git branch -D develop
3、切换到develop分支
git checkout develop
4、git三连
git merge --squash develop_merge
git add .
git commit -m "描述"
其中--squash命令的作用是将develop_merge分支上的几次commit"压缩"后合并到develop分支。
5、最后我们推送代码到远端仓库
git push origin HEAD:refs/for/develop