如何更改一系列提交的作者?


当前回答

最快、最简单的方法是使用gitrebase的--exec参数:

git rebase -i -p --exec 'git commit --amend --reset-author --no-edit'

这将创建如下所示的待办事项列表:

pick ef11092 Blah blah blah
exec git commit --amend --reset-author --no-edit
pick 52d6391 Blah bloh bloo
exec git commit --amend --reset-author --no-edit
pick 30ebbfe Blah bluh bleh
exec git commit --amend --reset-author --no-edit
...

这将自动运行,当您有数百次提交时,这将正常运行。

其他回答

我想对@Rognon的回答进行修改。如果所选答案或其他答案不适用于您(在我的特定问题中就是这样),则此答案只是另一种选择:

目标:您将在所有历史记录中用正确的作者修复一个或多个作者,并且您将获得无重复的干净历史记录。此方法的工作原理是将“master”分支替换为“clean”分支(它不使用merge/rebase)

注意:任何使用“主”存储库的人都可能需要在推送之前再次签出(执行这些步骤后),因为合并可能会失败。

我们将使用一个名为“clean”的新分支来执行操作(假设您想修复“master”):

git checkout-b clean

(确保您处于“干净”分支:git分支)

修改以下脚本(替换电子邮件地址和名称)。请注意,此脚本需要两个错误的电子邮件/作者(例如),因此如果您只需要修复一个作者,则可以删除条件的第二部分或将其保持原样(因为它将被忽略,因为它不匹配)。

执行脚本。

#/bin/bash

git filter-branch --force --commit-filter '
        if [ "$GIT_COMMITTER_EMAIL" = "wrong1@example.com" -o "$GIT_COMMITTER_EMAIL" = "wrong2@example.com" ];
        then
                export GIT_COMMITTER_NAME="John Doe";
                export GIT_AUTHOR_NAME="John Doe";
                export GIT_COMMITTER_EMAIL="correct@example.com";
                export GIT_AUTHOR_EMAIL="correct@example.com";
        fi;
        git commit-tree "$@"
' --tag-name-filter cat -- --all

它必须报告:Ref'refs/heads/clean'被重写。如果报告“未更改”,则脚本中输入的电子邮件可能是错误的。

使用:git log确认历史已更正

如果您正在使用github/gitlab(建议=安全):

在remote中创建“clean”分支:

git push—设置上游原点清洁

将“clean”分支设置为默认分支删除“master”(在执行此操作之前,确保一切都符合预期)。基于“clean”分支创建一个新分支“master”。确认一切正常后,您可以立即删除“clean”分支(也可以重命名它)。

如果您没有使用github/gitlab,或者您更喜欢使用命令:

从本地删除主分支:

git分支-d主

重命名分支:

gitbranch-m清理主机

推它(确保你的“主人”没有受到保护)

git push—强制原始主机

最快、最简单的方法是使用gitrebase的--exec参数:

git rebase -i -p --exec 'git commit --amend --reset-author --no-edit'

这将创建如下所示的待办事项列表:

pick ef11092 Blah blah blah
exec git commit --amend --reset-author --no-edit
pick 52d6391 Blah bloh bloo
exec git commit --amend --reset-author --no-edit
pick 30ebbfe Blah bluh bleh
exec git commit --amend --reset-author --no-edit
...

这将自动运行,当您有数百次提交时,这将正常运行。

对于所有提交,我的解决方案:

git rebase -i --root -x "git commit --amend --author 'bedorlan <bedorlan@gmail.com>' --no-edit"

使用交互式rebase,您可以在每次提交后放置一个修改命令。例如:

pick a07cb86 Project tile template with full details and styling
x git commit --amend --reset-author -Chead

对于单个提交:

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

(摘自阿斯梅勒的回答)