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


当前回答

如果您想(轻松)更改当前分支的作者,我会使用类似的方法:

# update author for everything since origin/master
git rebase \
  -i origin/master \
  --exec 'git commit --amend --no-edit --author="Author Name <author.name@email.co.uk>"'

其他回答

对于单个提交:

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

(摘自阿斯梅勒的回答)

正如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

我应该指出,如果唯一的问题是作者/电子邮件与您平时的不同,这不是问题。正确的修复方法是在目录底部创建一个名为.mailmap的文件,行如下

Name you want <email you want> Name you don't want <email you don't want>

从那时起,像git shortlog这样的命令会认为这两个名称是相同的(除非你明确告诉他们不要这样做)。看见https://schacon.github.io/git/git-shortlog.html了解更多信息。

这与这里所有其他解决方案的优势在于,您不必重写历史,如果您有上游,这可能会导致问题,并且总是意外丢失数据的好方法。

当然,如果你以自己的身份提交了某件事,而它真的应该是其他人,而此时你不介意改写历史,出于归因的目的,更改提交作者可能是一个好主意(在这种情况下,我会在这里告诉你我的另一个答案)。

对于重置当前用户和当前时间戳的所有提交(包括首次提交):

git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"
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#(循环将继续更新上次提交)