如文章标题所示

1、先看日志,输入git log

commit 98f37bf5609beab41d95824e4cf0f71b0db183b6 (HEAD -> hotfix/xxx)
Author: xxx <xxx.gmail.com>
Date:   Thu Nov 28 15:15:07 2024 +0800
    hotfix:正常的commit信息

commit 81ccae898b2fd52fe4a52d59bfbddc120a9dae80
Author: xxx <xxx.gmail.com>
Date:   Thu Nov 28 15:05:39 2024 +0800
    hotfix:正常的commit信息

commit 7c541d7ec2707058ff5798e6df0b353ccd3fa698
Author: xxx <xxx.gmail.com>
Date:   Thu Nov 28 10:05:46 2024 +0800
    hotfic:错误的commit信息

这里我们最近的第三次的提交信息的hotfix写成了hotfic,错误了,我们要修改这条commit信息

2、终端执行命令如下,其中HEAD~3表示选择修改最近的3条commit信息,假如你的错误是最近的一次的commit,就写HEAD~1或者HEAD^

git rebase -i HEAD~3
可以看到下面的结果
pick 7c541d7ec2 hotfix:正常的commit信息
pick 81ccae898b hotfix:正常的commit信息
pick 98f37bf560 hotfic:错误的commit信息

# Rebase e0a24a9a9c..98f37bf560 onto e0a24a9a9c (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# 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.

3、按 i,然后上下左右键将光标放到第三个pick旁,将pick删掉换成edit,如下

pick 7c541d7ec2 hotfix:正常的commit信息
pick 81ccae898b hotfix:正常的commit信息
edit 98f37bf560 hotfic:错误的commit信息

# Rebase e0a24a9a9c..98f37bf560 onto e0a24a9a9c (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
#         create a merge commit using the original merge commit's
#         message (or the oneline, if no original merge commit was
#         specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# 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.
然后按esc,按 shift + :,输入 wq,按回车,结果如下:
Stopped at 98f37bf560...  hotfic:错误的commit信息
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

4、按照提示,执行git commit --amend

hotfic:错误的commit信息

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Nov 28 15:15:07 2024 +0800
将hotfic改成hotfix,然后保存,然后执行git rebase --continue
Successfully rebased and updated refs/heads/hotfix/xxx
如果有多条commit要改,就继续依次执行git commit --amend和git rebase --continue

5、最后执行git push -f推到远端就可以了

写在最后,如果在以上最后的git push -f之前的任何一步做错了想重新来,可以通过git reset的方式回退代码,再重新git pull,本地的commit信息的更改就作废了

但愿这篇文章会帮到你
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐