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


当前回答

以上所有答案都改写了存储库的历史。只要要更改的名称未被多个作者使用,特别是如果存储库已共享且提交已过时,我更愿意使用.mailmap,记录在https://git-scm.com/docs/git-shortlog.它允许将不正确的名称/电子邮件映射到正确的名称,而无需修改回购历史记录。您可以使用以下行:

Proper Name <proper@email.xx> <root@localhost>

其他回答

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"
'

你的问题很普遍。请参阅“使用Mailmap修复Git中的作者列表”

为了简单起见,我创建了一个脚本来简化这个过程:gitchangemail

将该脚本放到路径上后,可以发出以下命令:

更改当前分支上的作者匹配$git changemail-aold@email.com-n新名称-mnew@email.com更改<branch>和<branch2>上的作者和提交者匹配。将-f传递到筛选器分支以允许重写备份$git changemail-bold@email.com-n新名称-mnew@email.com---f&lt;分支>&lt;分支2>显示回购中的现有用户$git changemail--显示两者

顺便说一下,在进行更改后,使用:gitbackupclean从过滤器分支中清除备份

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

# 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>"'

我也想添加我的示例。我想用给定的参数创建一个bash_function。

这在mint-linux 17.3中有效

# $1 => email to change, $2 => new_name, $3 => new E-Mail

function git_change_user_config_for_commit {

 # defaults
 WRONG_EMAIL=${1:-"you_wrong_mail@hello.world"}
 NEW_NAME=${2:-"your name"}
 NEW_EMAIL=${3:-"new_mail@hello.world"}

 git filter-branch -f --env-filter "
  if [ \$GIT_COMMITTER_EMAIL = '$WRONG_EMAIL' ]; then
    export GIT_COMMITTER_NAME='$NEW_NAME'
    export GIT_COMMITTER_EMAIL='$NEW_EMAIL'
  fi
  if [ \$GIT_AUTHOR_EMAIL = '$WRONG_EMAIL' ]; then
    export GIT_AUTHOR_NAME='$NEW_NAME'
    export GIT_AUTHOR_EMAIL='$NEW_EMAIL'
  fi
 " --tag-name-filter cat -- --branches --tags;
}

更改最后N次提交的作者的单个命令:

git rebase -i HEAD~N -x "git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit"

笔记

用引用替换HEAD~N,直到您要重写提交的地方。这可以是哈希、HEAD~4、分支名称。。。--no edit标志确保gitcommit--modify不会要求额外的确认当您使用gitrebase-i时,您可以手动选择要更改作者的提交位置,

您编辑的文件将如下所示:

pick 897fe9e simplify code a little
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit
pick dc18f70 bugfix
exec git commit --amend --author 'Author Name <author.name@mail.example>' --no-edit

然后,您仍然可以修改一些行,以查看要更改作者的位置。这为您在自动化和控制之间提供了一个很好的中间地带:您可以看到将要运行的步骤,一旦您保存了所有内容,将立即应用。

请注意,如果您已经使用git-configuser.name<your_name>和git-config user.email<your_email>修复了作者信息,那么也可以使用以下命令:

git rebase -i HEAD~N -x "git commit --amend --reset-author --no-edit"