如何在 Git 中更改多次提交的作者和提交者姓名和电子邮件?

我正在学校的计算机上编写一个简单的脚本,并将更改提交到 Git(在我的笔式驱动器中的一个存储库中,从我家里的计算机克隆)。 几次提交后,我意识到我是以 root 用户身份提交的东西。

有什么办法可以将这些提交的作者更改为我的名字吗?

In the case where just the top few commits have bad authors, you can do this all inside git rebase -i using the exec command and the --amend commit, as follows:

git rebase -i HEAD~6 # as required

which presents you with the editable list of commits:

pick abcd Someone else's commit
pick defg my bad commit 1
pick 1234 my bad commit 2

Then add exec ... --author="..." lines after all lines with bad authors:

pick abcd Someone else's commit
pick defg my bad commit 1
exec git commit --amend --author="New Author Name <email@address.com>" -C HEAD
pick 1234 my bad commit 2
exec git commit --amend --author="New Author Name <email@address.com>" -C HEAD

save and exit editor (to run).

This solution may be longer to type than some others, but it's highly controllable - I know exactly what commits it hits.

Thanks to @asmeurer for the inspiration.

When taking over an unmerged commit from another author, there is an easy way to handle this.

git commit --amend --reset-author

I found the presented versions way to aggressive, especially if you commit patches from other developers, this will essentially steal their code.

The version below does work on all branches and changes the author and comitter separately to prevent that.

Kudos to leif81 for the all option.

#!/bin/bash

git filter-branch --env-filter ’
if [ “$GIT_AUTHOR_NAME” = “<old author>” ];
then
GIT_AUTHOR_NAME=“<new author>”;
GIT_AUTHOR_EMAIL=“<youmail@somehost.ext>”;
fi
if [ “$GIT_COMMITTER_NAME” = “<old committer>” ];
then
GIT_COMMITTER_NAME=“<new commiter>”;
GIT_COMMITTER_EMAIL=“<youmail@somehost.ext>”;
fi
’ – --all

</div>