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


当前回答

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

其他回答

最快、最简单的方法是使用gitrebase的--exec参数:

git rebase -i -p --exec 'git commit --amend --reset-author --no-edit'

这将创建如下所示的待办事项列表:

pick ef11092 Blah blah blah
exec git commit --amend --reset-author --no-edit
pick 52d6391 Blah bloh bloo
exec git commit --amend --reset-author --no-edit
pick 30ebbfe Blah bluh bleh
exec git commit --amend --reset-author --no-edit
...

这将自动运行,当您有数百次提交时,这将正常运行。

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

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

这是@Brian版本的更详细版本:

要更改作者和提交人,可以执行以下操作(在bash中可以使用换行符):

git filter-branch --env-filter '
    if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
    then
        GIT_COMMITTER_NAME="<New name>";
        GIT_COMMITTER_EMAIL="<New email>";
        GIT_AUTHOR_NAME="<New name>";
        GIT_AUTHOR_EMAIL="<New email>";
    fi' -- --all

您可能会遇到以下错误之一:

临时目录已存在以Refs/original开头的引用已存在(这意味着先前在存储库上运行了另一个过滤器分支,然后在refs/original处备份原始分支引用)

如果要在出现这些错误的情况下强制运行,请添加--force标志:

git filter-branch --force --env-filter '
    if [ "$GIT_COMMITTER_NAME" = "<Old name>" ];
    then
        GIT_COMMITTER_NAME="<New name>";
        GIT_COMMITTER_EMAIL="<New email>";
        GIT_AUTHOR_NAME="<New name>";
        GIT_AUTHOR_EMAIL="<New email>";
    fi' -- --all

可能需要对--all选项做一点解释:它使过滤器分支可以处理所有引用(包括所有分支)的所有修订。例如,这意味着标记也被重写,并且在重写的分支上可见。

一个常见的“错误”是使用HEAD,这意味着只过滤当前分支上的所有修订。然后,重写的分支中不存在标记(或其他引用)。

使用交互式rebase,您可以在每次提交后放置一个修改命令。例如:

pick a07cb86 Project tile template with full details and styling
x git commit --amend --reset-author -Chead

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

git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"