手记

修改最后一次提交

运用场景

对于最后一次提交,如果想修改提交信息,或者追加修改的文件,可以通过该指令去完成。

git commit --amend

修改提交信息

$ git checkout main
$ git commit --amend
第二次修改iss53

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Feb 21 16:09:29 2024 +0800
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
#       modified:   README.md
#
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
"~/git-practise/.git/COMMIT_EDITMSG" 13L, 325B

执行该指令,会启动文本编辑器,里面包含了最后一条提交信息,可以输入新的内容替换这条提交信息。
信息内容修改之后,按键盘 Esc,再按 Shift + ; ,输入 wq 保存退出。
最后,通过 git push 添加 -f--force 或者 --force-with-lease 选项,来强制推送本地提交历史覆盖服务器上的提交历史。

追加修改的文件

在最后一次提交完成后,因为忘记修改部分文件,所以想更改提交的快照,也可以通过该指令来完成。

$ git add .
$ git commit --amend
第二次修改iss53

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Feb 21 16:09:29 2024 +0800
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
#       modified:   README.md
#
# Changes not staged for commit:
#       modified:   README.md
#
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
~                                                                                                                                                       
"~/git-practise/.git/COMMIT_EDITMSG" 16L, 384B
0人推荐
随时随地看视频
慕课网APP