注意:此答案会更改 SHA1,因此在已推送的分支上使用它时要小心。如果您只想修复名称的拼写或更新旧电子邮件,Git 可以让您无需使用重写历史记录即可完成此操作.mailmap
. See 我的另一个答案 https://stackoverflow.com/a/9491696/161801.
使用变基
首先,如果您还没有这样做,您可能需要在 git-config 中修复您的名称:
git config --global user.name "New Author Name"
git config --global user.email "<[email protected] /cdn-cgi/l/email-protection>"
这是可选的,但它也将确保重置提交者名称,假设这就是您所需要的。
要使用变基重写一系列提交的元数据,请执行以下操作
git rebase -r <some commit before all of your bad commits> \
--exec 'git commit --amend --no-edit --reset-author'
--exec
将运行git commit
每次提交被重写后的步骤(就像你跑了git commit && git rebase --continue
反复)。
如果您还想更改第一个提交(也称为“根”提交),则必须添加--root
到变基调用。
这会将提交者和作者更改为您的user.name
/user.email
配置。如果您不想更改该配置,可以使用--author "New Author Name <[email protected] /cdn-cgi/l/email-protection>"
代替--reset-author
。请注意,这样做会not更新提交者——仅更新作者。
单次提交
如果您只想更改最近的提交,则不需要变基。只需修改提交:
git commit --amend --no-edit --reset-author
整个项目历史
git rebase -r --root --exec "git commit --amend --no-edit --reset-author"
对于较旧的 Git 客户端(2020 年 7 月之前)
-r,--rebase-merges
对你来说可能不存在。作为替代品,您可以使用-p
。注意-p
存在严重问题,现已弃用。