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


当前回答

更改最后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"

其他回答

通过Amend更改提交作者姓名和电子邮件,然后用新提交替换旧提交:$git checkout<commit hash>#签出到需要修改的提交$gitcommit--modify--author“name”<author@email.com>“#更改作者姓名和电子邮件$git replace<旧提交哈希><新提交哈希>#用新提交替换旧提交$gitfilter分支--all#基于替换重写所有未来提交$git replace-d<old commit hash>#删除干净替换$git push-f origin HEAD#force push另一种方式回扣:$git rebase-i<good commit hash>#返回上一次提交#编辑器将打开,在提交更改作者之前将“pick”替换为“edit”$gitcommit--modify--author=“作者名称<author@email.com>“#更改作者姓名和电子邮件#保存更改并退出编辑器$git rebase--continue#完成rebase

这不是对你问题的回答,而是一个脚本,你可以用它来避免将来发生这种情况。它利用Git 2.9版以来可用的全局钩子,根据您所在的目录检查您的电子邮件配置:

#!/bin/sh
PWD=`pwd`
if [[ $PWD == *"Ippon"* ]] # 1)
then
  EMAIL=$(git config user.email)
  if [[ $EMAIL == *"Work"* ]] # 2)
  then
    echo "";
  else
    echo "Email not configured to your Work email in the Work directory.";
    git config user.email "youremail@youremail.com"
    echo "Git email configuration has now been changed to \"$(git config user$
    echo "\nPlease run your command again..."
    echo ''
    exit 1
  fi;
elif [[ $PWD == *"Personal"* ]]
then
  EMAIL=$(git config user.email)
  if [[ $EMAIL == "youremail@youremail.com" ]]
  then
    echo "";
  else
    echo "Email is not configured to your personal account in the Personal di$
    git config user.email "youremail@youremail.com"
    echo "Git email configuration has now been changed to \"$(git config user$
    echo "\nPlease run your command again..."
    echo ''
    exit 1;
  fi;
fi; 

它检查您当前的工作目录,然后验证您的git是否配置为正确的电子邮件。如果没有,它会自动更改。请在此处查看详细信息。

注意:这个答案会改变SHA1,所以在已经推送的分支上使用它时要小心。如果你只想修复一个名字的拼写或更新一封旧邮件,Git可以让你不用使用.mailmap重写历史。

使用回扣

首先,如果您还没有这样做,您可能需要在git-config中修复您的名称:

git config --global user.name "New Author Name"
git config --global user.email "<email@address.example>"

这是可选的,但它也将确保重置提交者名称,假设这是您所需要的。

要使用rebase重写一系列提交的元数据,请执行

git rebase -r <some commit before all of your bad commits> \
    --exec 'git commit --amend --no-edit --reset-author'

--exec将在每次提交被重写后运行gitcommit步骤(就像您运行gitcommit&&gitrebase一样——重复继续)。

如果您还想更改第一次提交(也称为“root”提交),则必须在rebase调用中添加--root。

这将把提交人和作者都更改为user.name/user.email配置。如果不想更改该配置,可以使用--author“New author Name”<email@address.example>“而不是--reset-author。请注意,这样做不会更新提交者,只更新作者。

单次提交

如果您只想更改最近的提交,则无需重新设置基准。只需修改承诺:

 git commit --amend --no-edit --reset-author

整个项目历史

git rebase -r --root --exec "git commit --amend --no-edit --reset-author"

对于较旧的Git客户端(2020年7月之前)

-r、 --您可能不存在重基合并。作为替换,可以使用-p。请注意,-p存在严重问题,现已弃用。

我想对@Rognon的回答进行修改。如果所选答案或其他答案不适用于您(在我的特定问题中就是这样),则此答案只是另一种选择:

目标:您将在所有历史记录中用正确的作者修复一个或多个作者,并且您将获得无重复的干净历史记录。此方法的工作原理是将“master”分支替换为“clean”分支(它不使用merge/rebase)

注意:任何使用“主”存储库的人都可能需要在推送之前再次签出(执行这些步骤后),因为合并可能会失败。

我们将使用一个名为“clean”的新分支来执行操作(假设您想修复“master”):

git checkout-b clean

(确保您处于“干净”分支:git分支)

修改以下脚本(替换电子邮件地址和名称)。请注意,此脚本需要两个错误的电子邮件/作者(例如),因此如果您只需要修复一个作者,则可以删除条件的第二部分或将其保持原样(因为它将被忽略,因为它不匹配)。

执行脚本。

#/bin/bash

git filter-branch --force --commit-filter '
        if [ "$GIT_COMMITTER_EMAIL" = "wrong1@example.com" -o "$GIT_COMMITTER_EMAIL" = "wrong2@example.com" ];
        then
                export GIT_COMMITTER_NAME="John Doe";
                export GIT_AUTHOR_NAME="John Doe";
                export GIT_COMMITTER_EMAIL="correct@example.com";
                export GIT_AUTHOR_EMAIL="correct@example.com";
        fi;
        git commit-tree "$@"
' --tag-name-filter cat -- --all

它必须报告:Ref'refs/heads/clean'被重写。如果报告“未更改”,则脚本中输入的电子邮件可能是错误的。

使用:git log确认历史已更正

如果您正在使用github/gitlab(建议=安全):

在remote中创建“clean”分支:

git push—设置上游原点清洁

将“clean”分支设置为默认分支删除“master”(在执行此操作之前,确保一切都符合预期)。基于“clean”分支创建一个新分支“master”。确认一切正常后,您可以立即删除“clean”分支(也可以重命名它)。

如果您没有使用github/gitlab,或者您更喜欢使用命令:

从本地删除主分支:

git分支-d主

重命名分支:

gitbranch-m清理主机

推它(确保你的“主人”没有受到保护)

git push—强制原始主机

如果您要修复的提交是最新的,并且只有几个,您可以使用git reset和git stash的组合,在配置正确的名称和电子邮件后再次提交。

顺序如下(对于2个错误的提交,没有挂起的更改):

git config user.name <good name>
git config user.email <good email>
git reset HEAD^
git stash
git reset HEAD^
git commit -a
git stash pop
git commit -a