提交提交后如何修复拼写错误

2025-06-08

提交提交后如何修复拼写错误

或者替代标题 - 我如何学会喜欢 Git rebase

刚开始用 Git 的时候,我被灌输了合并的思想,被告知永远不要使用变基(rebase)。变基会重写历史记录。Git 的本质就是追踪历史记录。因此,变基是糟糕的。

但是有一个工作流程真正巩固了我的转变,并已成为我代码编写过程的常规部分 - 在我的活动分支上进行重新定基。

即使我的代码编辑器里安装了 linters 和拼写检查扩展,我还是会时不时地发现提交到 git 的拼写错误。或者我会忘记某个残留文件中的更改。由于基本的推送工作流程现在已经成为了肌肉记忆,所以我会在注意到错误之前就推送提交。我会修复它,然后执行以下操作之一……

$ git commit -m "fix typo"

总的。

但是我们可以通过交互式变基来快速修复这个问题!

先修复

修复错误后,我会像平常一样暂存文件。然后-m,我会用选项标记提交,而不是像之前那样显示令人尴尬的消息--fixup。该命令需要一个提交的 SHA-1 值来附加修复。

$ git add .
$ git commit --fixup 710f0f8

另一个巧妙的技巧是将前一个提交称为当前提交的父提交。和 都可以,就像HEAD~引用上一个提交或当前提交的祖父提交一样。请注意不能互换。Git 甚至足够智能,能够找到提交消息的第一个单词。HEAD^HEAD~2~^

# these will all fixup your commit to the previous one.
$ git commit HEAD~
$ git commit HEAD^
$ git commit :/update

当我运行时git log,历史记录将如下所示:

f7f3f6d (HEAD) fixup! update variable name
310154e update variable name
a5f4a0d (master, origin/master, origin/HEAD) working code on master

让我们重新定位!

我们添加-i命令以在交互模式下运行变基,并提供要编辑的最后一个提交的父提交作为参数。我将此作为一条规则:在需要回溯的提交数量上加 1。添加命令--autosquash将拾取所有以 开头的提交fixup!,并使用填写的命令设置交互式变基会话。

$ git rebase -i --autosquash HEAD~3

该命令的结果将以升序排列您的所有提交(我的默认设置是在 vim 中打开),并显示 git 在运行提交时应执行的操作。请注意,最后一个提交已fixup附加命令。

pick a5f4a0d working code on master
pick 310154e update variable name
fixup f7f3f6d fixup! update variable name


# Rebase a5f4a0d..f7f3f6d onto a5f4a0d (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 <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# 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.
#
# 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

此时,如果我要进行最后的清理以推送到远程分支,我可能会reword添加提交消息,甚至可能将squash一些无关的提交放在一起。值得一读评论,因为这是一个非常强大的工作流程,也是fixup我最常用的。

完成 rebase 后,您的修订版本git log应该有一个新的单一提交,它将修复提交与前一个提交合并在一起。鞠躬并接受掌声

2231360 update variable name
a5f4a0d (master, origin/master, origin/HEAD) working code on master

自动挤压魔法

--autosquash也会使用 option 来获取提交--squash,但我倾向于不保留该消息,所以 fixup 对我来说就足够了。如果你有大量新代码,但只想要一个原子提交,Squash 可能是一个不错的选择。

您还可以设置以下 git 配置设置,以省略每次运行交互式 rebase 时必须包含 autosquash 选项。

$ git config --global rebase.autosquash true

我的设置始终将其设置为 true,这有助于使修复和压缩提交感觉像是第二天性,因为要进入 rebase 会话,我只需要输入git rebase -i HEAD~3或输入我认为需要清理的提交数量。

这就是我转换为 rebase 的方式!

其他有用的资源包括这个关于修改历史记录的 git 教程。消化完这个之后,我发现理解rebase 的完整文档就更容易了。

鏂囩珷鏉yu簮锛�https://dev.to/bellawoo/how-to-fix-a-typo-after-you-ve-already-pushed-your-commit-342l
PREV
2 个单元测试。0 个集成测试。
NEXT
如何在网站上添加暗模式切换。