我想改变历史上某个特定承诺的作者。这不是最新的承诺。

相关:如何更改多次提交的作者和提交人姓名/电子邮件?


当前回答

使用交互式回扣

git rebase -i -p <some HEAD before all of your bad commits>

然后在rebase文件中将所有错误提交标记为“edit”,当git要求您修改每个提交时

git commit --amend --author "New Author Name <email@address.com>"

编辑或关闭打开的编辑器,然后执行

git rebase --continue

以继续重新启动。

您可以通过附加--no edit来跳过在此处完全打开编辑器,这样命令将是:

git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continue

单次提交

正如一些评论所指出的,如果您只想更改最近的提交,那么rebase命令是不必要的。就这样吧

git commit --amend --author "New Author Name <email@address.com>"

这会将author更改为指定的名称,但committer将设置为gitconfig user.name和gitconfig user.email中配置的用户。如果要将committer设置为指定的值,这将同时设置author和committer:

git -c user.name="New Author Name" -c user.email=email@address.com commit --amend --reset-author

其他回答

为了进一步解释Eugen Konkov的回答,从根提交开始,使用--root标志。--noedit标志也很有用,因为使用它,每次提交都不会提示您进入编辑器。

git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"

提交时间:

要修复所有提交的作者,您可以从@Amber的回答中应用命令:

git commit --amend --author="Author Name <email@address.com>"

或者,要重复使用您的姓名和电子邮件,您只需写下:

git commit --amend --author=Eugen

命令后提交:

例如,从4025621开始更改所有:

您必须运行:

git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621

注意:要包含包含空格(如姓名和电子邮件地址)的作者,作者必须用转义引号括起来。例如:

git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621

或将此别名添加到~/.gitconfig中:

[alias]
    reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --

然后运行:

git reauthor 4025621 Eugen

在全球范围内更改提交人姓名和电子邮件:

$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"

更改每个存储库的提交人名称和电子邮件:

$ git config user.name "John Doe"
$ git config user.email "john@doe.org"

仅为下一次提交更改作者信息:

$ git commit --author="John Doe <john@doe.org>"

提示:对于其他情况和阅读更多信息,请阅读帖子参考。

如果要更改的提交不是最后一次提交,请执行以下步骤。如果您的提交位于不同的分支,那么首先切换到该分支。

git签出分支名称

在要更改的提交之前查找提交并查找其哈希。然后发出rebase命令。

git rebase-i-p提交哈希

然后将打开一个编辑器,并输入“edit”以查看要更改的提交。让其他人保留默认的“选择”选项。一旦更改,输入“esc”键和wq!退出。

然后发出带有修正选项的gitcommit命令。

gitcommit--modify--author=“用户名电子邮件”--无编辑

然后发出以下命令。

git rebase—继续

在本地存储库中更新提交作者后,将更改推送到远程存储库。

如果您使用的是集中存储库,Amber的答案还有一个步骤:

gitpush-f强制更新中央存储库。

请注意,在同一分支上工作的人不多,因为这会破坏一致性。