一、这篇文章的起因是提交代码时报的一行错误:
- remote: Push to refs/for/dev to create a review, or get 'Push' rights to update the branch.
乍一看是需要先Push到refs/for/dev创建审查或push权限。 - 好吧,权限是不可能有权限的,只能提交到refs/for/dev地址上了:
git push origin HEAD:refs/for/dev
你以为这就完了吗,如果只是这么简单那就不必记录了。
二、第二个错误:remote: ERROR: commit xxxxxx: missing Change-Id in message footer
好在它有给提示
remote: Resolving deltas: 100%
remote: ERROR: commit xxxxxx: missing Change-Id in message footer
remote:
remote: Hint: to automatically insert a Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P xxxx username@127.0.0.1:hooks/commit-msg ${gitdir}/hooks/
remote: or, for http(s):
remote: f="$(git rev-parse --git-dir)/hooks/commit-msg"; curl -o "$f" http://127.0.0.1/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:
To ssh://127.0.0.1/Test/test
! [remote rejected] HEAD -> refs/for/dev (commit xxxxxx: missing Change-Id in message footer)
error: failed to push some refs to 'ssh://127.0.0.1/Test/test'
- 直接根据提示执行:
gitdir=$(git rev-parse --git-dir); scp -p -P xxxx username@127.0.0.1:hooks/commit-msg ${gitdir}/hooks/
好家伙,果然不行:
$ gitdir=$(git rev-parse --git-dir); scp -p -P xxxx username@127.0.0.1:hooks/commit-msg ${gitdir}/hooks/
subsystem request failed on channel 0
scp: Connection closed
问了下度娘,还好有人给出了答案: gitdir=$(git rev-parse --git-dir); scp -O -p -P xxxx username@172.0.0.1:hooks/commit-msg ${gitdir}/hooks/
只是添加了一个-O选项就好了,可能Gerrit使用的scp协议比较旧导致的,参考。
- 然后接着根据提示执行:
git commit --amend --no-edit
这句还挺顺利,不过它确实也没做什么,只是重新用--amend提交了一次而已。
然后呢,这就行了吗?
- 按照正常逻辑到这里再调用一下:
git push origin HEAD:refs/for/dev确实就可以了。
但显然我不是正常情况,由于已经commit了多次,所以虽然最后一次提交有了Change-Id,但是之前的提交依然没有。
三、撤销重提
这里有两种方式:
- 一种是用
git rebase -i进入rebase模式来给每次提交用git commit --amend --no-edit来添加Change-Id。- 具体操作就是通过
git rebase -i进入rebase模式,然后在需要添加change_id 的提交将pick改为edit(编辑)或reword(仅修改提交信息),然后用:wq保存 - 再使用
git commit --amend --no-edit添加Change-Id(使用reword则直接:wq保存),然后git rebase --continue跳到下一条(或者直接合并执行git commit --amend --no-edit && git rebase --continue),如此往复执行。 - 当然要跳过可以用
git rebase --skip退出用git rebase --abort,本人不是用该方法,未验证,参考。 - 当要修改的内容为
merge提交时git rebase -i可能没显示merge的提交,需要单独处理- 应采用
git rebase -i --rebase-merges <commit_id>来开启修改 - 将
merge -C <原merge提交的哈希> <merge的说明>中的大写-C改为小写-c,如:merge -c <原merge提交的哈希> <merge的说明> - 保存后使用
git rebase --continue即可修改merge的commit信息,流程和上面一样
- 应采用
- 具体操作就是通过
- 另外一种就比较简单粗暴了,直接用
git reset --soft <commit_id>撤回到有change-id的提交记录,然后用git commit -am "提交信息"重新提交。就是这么简单- 如果不小心提交错了可以用
git reset HEAD@{1}撤回。
- 如果不小心提交错了可以用
四、常用命令
- 修改作者:git commit --amend --reset-author 参考