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

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


当前回答

使用交互式回扣

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

其他回答

溶液

安装git filter repo(git项目建议在filter分支上安装filter repo)$PACKAGE_TOOL安装git筛选器repo在git存储库的根目录中创建一个文件.mailmap,其中包含新名称<new@ema.il> <old@ema.il>运行gitfilter repo--使用mailmap


更多详细信息

gitfilter repo在其文档中将其列为示例不要像上面的示例那样替换名称和电子邮件,请查看gitmailmap文档中的其他示例您可以通过使用参数--mailmap<filename>调用gitfilter repo来指定mailmap文件,而不是默认使用名为.mailmap的文件。关于如何进一步过滤分支/标签/任何内容的更多示例,可以在gitfilter repo项目的README.md中找到。

如果您需要更改的是上一次提交的作者,而没有其他人正在使用您的存储库,您可以使用以下方法撤消上一次的提交:

git push -f origin last_commit_hash:branch_name 

更改提交的作者名称:

git commit --amend --author "type new author here"

退出打开的编辑器并再次推送代码:

git push

对于合并提交消息,我发现我不能通过使用rebase来修改它,至少在gitlab上是这样。它将合并显示为提交,但我无法重新基于该#sha。我发现这篇文章很有用。

git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch

这三行代码完成了更改合并提交消息的工作(如作者)。

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

git签出分支名称

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

git rebase-i-p提交哈希

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

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

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

然后发出以下命令。

git rebase—继续

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

提交时间:

要修复所有提交的作者,您可以从@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