Git学习笔记(五)——修改commit message

在使用git过程中,经常会需要修改提交message的情况,下面是借助rebase对本地提交message修改的示例。

一、修改最近一次的提交的message

git commit -amend

修改完成后:wq保存退出即可。

二、修改历史提交的message

通过rebase方式修改历史commit message,指令格式如下:

git rebase -i HASH

指定需要修改commit的前一次commit id,将需要修改messagecommit修改Commands为reword,无需修改的保持pick
以下演示,将第二个的commitmessageadd 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
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值