背景
近期在做一些開(kāi)源項(xiàng)目的時(shí)候,提交 pull request 有些會(huì)要求提交 DCO 信息,用于 CI 的驗(yàn)證.簡(jiǎn)單來(lái)說(shuō)就是,要在提交信息中增加例如
Signed-off-by: Random J Developer <random@developer.example.org>
的提交信息,前面文檔也提到過(guò),增加這行信息其實(shí)很簡(jiǎn)單,就是在提交 git commit 的時(shí)候,加上-s 參數(shù)即可.
$ git commit -s -m 'This is my commit message'
但是我們今天要解決的問(wèn)題是, git 已經(jīng)提交了,如何去修改以前的提交信息,讓他也附加上這樣的提交信息,如果不修改,那么 CI 校驗(yàn) 一定過(guò)不了,在 有 DCO 的項(xiàng)目中,會(huì)有如下的提示:

項(xiàng)目 owner 一般會(huì)要求你修改歷史的 commit 信息,必須通過(guò) CI 校驗(yàn).
解決方法
修改最新的 log
- 如果只修改最新一條提交信息的 log 內(nèi)容,直接使用命令
git commit --amend就可以完成修改啦, 可以直接參考 git 文檔,也就是下面的 重寫(xiě)歷史 功能,比較簡(jiǎn)單.
修改歷史的 log
如果要修改歷史的版本信息(非最新一條),會(huì)稍稍麻煩一點(diǎn),不過(guò)也可以搞定,這里用到的方法就是 git命令的重寫(xiě)歷史功能.
- 假定我們現(xiàn)在的提交信息是這樣的
$ git log
commit 9ac1179332670365a3e5ea00a6486998eb66db7a (HEAD -> fix_aop_no_class_defined, origin/fix_aop_no_class_defined)
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 19:58:23 2018 +0800
update test case
Signed-off-by: candyleer <295198088@qq.com>
commit 223fc80d17273b19238dcb648e6d6eefc579de0f
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 18:47:50 2018 +0800
unit test case
Signed-off-by: candyleer <295198088@qq.com>
commit 2275781a0d75add037721832bd68c1a8edb3813e
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 18:29:29 2018 +0800
should find method from parent
一共有三條歷史提交信息,第 二三條都已經(jīng)加上了 DCO 信息,現(xiàn)在想在第一條(最老的一條)的提交信息上面也加上的話.我們可以按照上述文檔操作:
- 執(zhí)行 git 命令, 修改近三次的信息
$ git rebase -i HEAD~3
將會(huì)得到如下的信息,這里的提交日志是和git log倒敘排列的,我們要修改的日志信息位于第一位.
1 pick 2275781 should find method from parent
2 pick 223fc80 unit test case
3 pick 9ac1179 update test case
4
5 # Rebase 79db0bd..9ac1179 onto 79db0bd (3 commands)
6 #
7 # Commands:
8 # p, pick = use commit
9 # r, reword = use commit, but edit the commit message
10 # e, edit = use commit, but stop for amending
11 # s, squash = use commit, but meld into previous commit
12 # f, fixup = like "squash", but discard this commit's log message
13 # x, exec = run command (the rest of the line) using shell
14 # d, drop = remove commit
15 #
16 # These lines can be re-ordered; they are executed from top to bottom.
17 #
18 # If you remove a line here THAT COMMIT WILL BE LOST.
19 #
20 # However, if you remove everything, the rebase will be aborted.
21 #
22 # Note that empty commits are commented out
- 我們現(xiàn)在要修改修改要
should find method from parent這條日志,那么修改的日志為,將第一個(gè)pick修改為edit, 然后:wq退出.
1 edit 2275781 should find method from parent
2 pick 223fc80 unit test case
3 pick 9ac1179 update test case
將會(huì)看到如下信息,意思就是如果要改日志,執(zhí)行git commit --amend,如果修改完成后,執(zhí)行git rebase --continue
client_java git:(fix_aop_no_class_defined) git rebase -i HEAD~3
Stopped at 2275781... should find method from parent
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
? client_java git:(2275781)
- 正式修改,執(zhí)行命令,
-s就是自動(dòng)加上Signed-off-by:
$ git commit --amend -s
client_java git:(63b2cfd) git commit --amend -s
[detached HEAD c46b30e] 1should find method from parent
Date: Mon Apr 16 18:29:29 2018 +0800
1 file changed, 4 insertions(+), 1 deletion(-
修改完成后,:wq 退出,然后完成此次 log 的rebase
$ git rebase --continue
client_java git:(c46b30e) git rebase --continue
Successfully rebased and updated refs/heads/fix_aop_no_class_defined.
- 這樣本地修改就完成啦,用
git log再看下:
commit 449efc747ffb85567667745b978ed7e3418cfe27 (HEAD -> fix_aop_no_class_defined)
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 19:58:23 2018 +0800
update test case
Signed-off-by: candyleer <295198088@qq.com>
commit 69237c0bd48439ea0d8b87bf2c7c7ac4786c66d4
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 18:47:50 2018 +0800
unit test case
Signed-off-by: candyleer <295198088@qq.com>
commit c46b30e456af6ecdf4a629a485e5efe5485e52b1
Author: candyleer <295198088@qq.com>
Date: Mon Apr 16 18:29:29 2018 +0800
1should find method from parent
Signed-off-by: candyleer <295198088@qq.com>
所有信息都有Signed-off-by:這個(gè)參數(shù)了
- 最后push 到遠(yuǎn)程倉(cāng)庫(kù),所有的 DCO 就都可以加上啦,
-f強(qiáng)制推送
$ git push origin <you_branch_name> -f
再次查看 github 的 CI,DCO 相關(guān)的就check pass啦

- over