在使用git过程中,经常会需要修改提交message的情况,下面是借助rebase
对本地提交message修改的示例。
一、修改最近一次的提交的message
git commit -amend
修改完成后:wq
保存退出即可。
二、修改历史提交的message
通过rebase
方式修改历史commit message
,指令格式如下:
git rebase -i HASH
指定需要修改commit的前一次commit id
,将需要修改message
的commit
修改Commands为reword
,无需修改的保持pick
。
以下演示,将第二个的commit
的message
从add line
改为add a line
。
[root@VM-8-11-centos git]# ls
hello.sh
[root@VM-8-11-centos git]# git status
# On branch master
nothing to commit, working directory clean
[root@VM-8-11-centos git]# git log --graph
* commit 9e358c02dfcc41ef519ef013b5745e912f0acf11
| Author: you <you@local.com>
| Date: Mon May 23 11:51:55 2022 +0800
|
| add change world.
|
* commit bdca130a4a7523c989520bfd6510dd3091726cba
| Author: you <you@local.com>
| Date: Mon May 23 11:51:11 2022 +0800
|
| add line
|
* commit 3055991d336b7d956f125ced593a01a7af8a9814
Author: you <you@local.com>
Date: Mon May 23 11:50:30 2022 +0800
init
[root@VM-8-11-centos git]# git rebase -i 3055991d336b7d956f125ced593a01a7af8a9814
## 进入到交互式控制中,修改对应提交的Commands为reword,:wq保存
reword bdca130 add line
pick 9e358c0 add change world.
# Rebase 3055991..9e358c0 onto 3055991
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
######## 退出后,自动进入到message编辑,修改message,同样以:wq保存退出
add a line
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached from 3055991
# You are currently editing a commit while rebasing branch 'master' on '3055991'.
#
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# modified: hello.sh
#
##### 退出后,控制台输出成功变更信息
[detached HEAD 6f4306f] add a line
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.
完成rebase
后,可以发现commit id
发生了变化,同时前面修改为reword
的提交 message也发生变化
[root@VM-8-11-centos git]# git log --graph
* commit dd79a867725fa6819ddf9e327b50bc43e6ac2c99
| Author: you <you@local.com>
| Date: Mon May 23 11:51:55 2022 +0800
|
| add change world.
|
* commit 6f4306ffdb4a0c7fbeca9e2af295674bf79372f4
| Author: you <you@local.com>
| Date: Mon May 23 11:51:11 2022 +0800
|
| add a line
|
* commit 3055991d336b7d956f125ced593a01a7af8a9814
Author: you <you@local.com>
Date: Mon May 23 11:50:30 2022 +0800
init