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


当前回答

正如docgnome所提到的,重写历史是危险的,会破坏其他人的知识库。

但是,如果您真的想这样做,并且您处于bash环境中(在Linux和Windows中没有问题,您可以使用git bash,这是安装git时提供的),请使用gitfilter分支:

git filter-branch --env-filter '
  if [ $GIT_AUTHOR_EMAIL = bad@email ];
    then GIT_AUTHOR_EMAIL=correct@email;
  fi;
export GIT_AUTHOR_EMAIL'

要加快速度,可以指定要重写的修订范围:

git filter-branch --env-filter '
  if [ $GIT_AUTHOR_EMAIL = bad@email ];
    then GIT_AUTHOR_EMAIL=correct@email;
  fi;
export GIT_AUTHOR_EMAIL' HEAD~20..HEAD

其他回答

git rebase -i YOUR_FIRTS_COMMIT_SHA^

while true; do git commit --amend --author="Name Surname <email@example.com>" --no-edit && git rebase --continue; done

在重基完成后按^C#(循环将继续更新上次提交)

git过滤器分支的一个更安全的替代方案是git文档建议的过滤器回购工具。

git filter-repo --commit-callback '
  old_email = b"your-old-email@example.com"
  correct_name = b"Your Correct Name"
  correct_email = b"your-correct-email@example.com"
  
  if commit.committer_email == old_email :
    commit.committer_name = correct_name
    commit.committer_email = correct_email

  if commit.author_email == old_email : 
    commit.author_name = correct_name
    commit.author_email = correct_email
  '

上述命令反映了此脚本中使用的逻辑,但使用过滤器repo而不是过滤器分支。

提交后回调选项的代码体基本上是用于处理提交的python代码。您可以在这里用python编写自己的逻辑。请在此处查看有关提交对象及其属性的更多信息。

由于filter repo工具未与git捆绑,您需要单独安装它。

请参阅先决条件和安装指南

如果您有一个python-env>=3.5,可以使用pip来安装它。

pip3 install git-filter-repo

注意:强烈建议在新克隆上尝试使用过滤器回购工具。操作完成后,也会移除遥控器。阅读更多有关为什么删除遥控器的信息。另请阅读INTERNALS部分中此工具的限制。

GitHub最初有一个很好的解决方案(断开链接),它是以下shell脚本:

#!/bin/sh

git filter-branch --env-filter '

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "your@email.to.match.example" ]
then
    cn="Your New Committer Name"
    cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "your@email.to.match.example" ]
then
    an="Your New Author Name"
    am="Your New Author Email"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'

注意,git存储了两个不同的电子邮件地址,一个是提交人(提交更改的人),另一个是作者(编写更改的人员)。

提交者信息在大多数地方都不会显示,但您可以通过git-log-1--format=%cn,%ce(或使用show而不是log来指定特定的提交)看到它。

虽然更改上一次提交的作者与gitcommit一样简单--修改--author“作者名称”<email@example.com>“,没有一个liner或argument对提交者信息做同样的处理。

解决方案是(暂时或不)更改您的用户信息,然后修改提交,这将更新提交者到您的当前信息:

git config user.email my_other_email@example.com 
git commit --amend

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

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