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


当前回答

你的问题很普遍。请参阅“使用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从过滤器分支中清除备份

其他回答

您还可以执行以下操作:

git filter-branch --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
        then
                GIT_COMMITTER_NAME="<New Name>";
                GIT_AUTHOR_NAME="<New Name>";
                GIT_COMMITTER_EMAIL="<New Email>";
                GIT_AUTHOR_EMAIL="<New Email>";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

注意,如果在Windows命令提示符中使用此命令,则需要使用“而不是”:

git filter-branch --commit-filter "
        if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
        then
                GIT_COMMITTER_NAME="<New Name>";
                GIT_AUTHOR_NAME="<New Name>";
                GIT_COMMITTER_EMAIL="<New Email>";
                GIT_AUTHOR_EMAIL="<New Email>";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi" HEAD

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

我们今天遇到了一个问题,作者名中的UTF8字符在构建服务器上造成了麻烦,因此我们必须重写历史记录来纠正这个问题。所采取的步骤包括:

步骤1:按照此处的说明,更改所有未来提交的git用户名:https://help.github.com/articles/setting-your-username-in-git/

步骤2:运行以下bash脚本:

#!/bin/sh

REPO_URL=ssh://path/to/your.git
REPO_DIR=rewrite.tmp

# Clone the repository
git clone ${REPO_URL} ${REPO_DIR}

# Change to the cloned repository
cd ${REPO_DIR}

# Checkout all the remote branches as local tracking branches
git branch --list -r origin/* | cut -c10- | xargs -n1 git checkout

# Rewrite the history, use a system that will preseve the eol (or lack of in commit messages) - preferably Linux not OSX
git filter-branch --env-filter '
OLD_EMAIL="me@something.com"
CORRECT_NAME="New Me"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
fi
' --tag-name-filter cat -- --branches --tags

# Force push the rewritten branches + tags to the remote
git push -f

# Remove all knowledge that we did something
rm -rf ${REPO_DIR}

# Tell your colleagues to `git pull --rebase` on all their local remote tracking branches

快速概述:将存储库签出到临时文件,签出所有远程分支,运行将重写历史记录的脚本,强制推送新状态,并告诉所有同事重新启动数据库以获取更改。

我们在OS X上运行它时遇到了问题,因为它在提交消息中以某种方式打乱了行结尾,所以我们不得不在Linux机器上重新运行它。

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

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

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