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


当前回答

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#(循环将继续更新上次提交)

其他回答

我也想添加我的示例。我想用给定的参数创建一个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;
}

如果您将Eclipse与EGit一起使用,那么有一个非常简单的解决方案。假设:您在本地分支“local_master_user_x”中有提交,由于用户无效,无法将其推送到远程分支“master”。

签出远程分支“master”选择“local_master_user_x”包含更改的项目/文件夹/文件右键单击-替换为-分支-“local_master_user_x”再次提交这些更改,这次是以正确的用户身份提交到本地分支“master”中推到远程“主机”

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

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

一个带过滤器的衬里:

您可以使用gitfilter repo的回调功能(推荐替换过滤器分支)来更改与所有提交相关的名称和电子邮件:

git filter-repo --name-callback 'return b"New Name"' --email-callback 'return b"newemail@gmail.com"'

这比使用过滤器分支的解决方案性能更高,可能更可靠。

请注意,上面的命令会更改所有提交的作者(和提交者),如果您想有效地“编辑”某个作者,并且只修改该特定作者的提交,请使用--commit回调选项,如下所示:

git filter-repo --commit-callback '
old_email = b"oldemail@gmail.com"
new_email = b"newemail@gmail.com"
new_name = b"New Author"

if commit.author_email == old_email:
    commit.author_email = new_email
    commit.author_name = new_name

if commit.committer_email == old_email:
    commit.committer_email = new_email
    commit.committer_name = new_name
'

(只需将上面命令中的old_email、new_email和new_name变量更改为正确的值。)

我应该指出,如果唯一的问题是作者/电子邮件与您平时的不同,这不是问题。正确的修复方法是在目录底部创建一个名为.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了解更多信息。

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

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