我想改变历史上某个特定承诺的作者。这不是最新的承诺。

相关:如何更改多次提交的作者和提交人姓名/电子邮件?


当前回答

如果您只想更改上次提交的作者,可以执行以下操作:

将电子邮件重置为全局配置:git-config--全局用户电子邮件example@email.com现在重置提交的作者,无需编辑:gitcommit--修改--重置作者--无编辑

注意,这也会更改作者时间戳。

其他回答

在执行git rebase-i时,文档中有一个有趣的部分:

如果要将两个或多个提交合并为一个,请将第二次和后续提交的命令“pick”替换为“squash”或“fixup”。如果提交的作者不同,则折叠提交将归于第一次提交的作者。折叠提交的建议提交消息是第一次提交的提交消息和使用“squash”命令的提交消息的串联,但使用“fixup”命令省略了提交的提交信息。

如果你有A-B-C-D-E-F病史,并且您希望更改提交B和D(=2个提交),

那么您可以执行以下操作:

git-config user.name“更正新名称”git-config user.email“correct@new.email"创建空提交(每个提交一个):你需要一条消息来重新设置基础gitcommit--允许空-m“空”启动重新启动操作git rebase-i B^B^选择B的父级。您将需要在每次提交之前放置一个空提交以进行修改你会想把这些换成壁球。

git rebase-i B^将为您提供的示例:

pick sha-commit-B some message
pick sha-commit-C some message
pick sha-commit-D some message
pick sha-commit-E some message
pick sha-commit-F some message
# pick sha-commit-empty1 empty
# pick sha-commit-empty2 empty

将其更改为:

# change commit B's author
pick sha-commit-empty1 empty
squash sha-commit-B some message
# leave commit C alone
pick sha-commit-C some message
# change commit D's author
pick sha-commit-empty2 empty
squash sha-commit-D some message
# leave commit E-F alone
pick sha-commit-E some message
pick sha-commit-F some message

它将提示您编辑消息:

# This is a combination of 2 commits.
# The first commit's message is:

empty

# This is the 2nd commit message:

...some useful commit message there...

你可以删除前几行。

提交时间:

要修复所有提交的作者,您可以从@Amber的回答中应用命令:

git commit --amend --author="Author Name <email@address.com>"

或者,要重复使用您的姓名和电子邮件,您只需写下:

git commit --amend --author=Eugen

命令后提交:

例如,从4025621开始更改所有:

您必须运行:

git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621

注意:要包含包含空格(如姓名和电子邮件地址)的作者,作者必须用转义引号括起来。例如:

git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621

或将此别名添加到~/.gitconfig中:

[alias]
    reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --

然后运行:

git reauthor 4025621 Eugen

作为Eugen Konkov回答的补充,可以保留提交/作者日期。要只修改作者而不修改最后三次提交的日期,请使用

git rebase --onto HEAD~3 --exec 'GIT_COMMITTER_DATE="$(git log -n 1 --format=%aD)" git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"' HEAD~3

然后用力推

git push --force-with-lease

你所链接的问题中的答案是好答案,涵盖了你的情况(另一个问题更为一般,因为它涉及重写多次提交)。

作为尝试gitfilter分支的借口,我编写了一个脚本来重写给定提交的作者名和/或作者电子邮件:

#!/bin/sh

#
# Change the author name and/or email of a single commit.
#
# change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
#
#     If -f is supplied it is passed to "git filter-branch".
#
#     If <branch-to-rewrite> is not provided or is empty HEAD will be used.
#     Use "--all" or a space separated list (e.g. "master next") to rewrite
#     multiple branches.
#
#     If <new-name> (or <new-email>) is not provided or is empty, the normal
#     user.name (user.email) Git configuration value will be used.
#

force=''
if test "x$1" = "x-f"; then
    force='-f'
    shift
fi

die() {
    printf '%s\n' "$@"
    exit 128
}
targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
br="${2:-HEAD}"

TARG_COMMIT="$targ"
TARG_NAME="${3-}"
TARG_EMAIL="${4-}"
export TARG_COMMIT TARG_NAME TARG_EMAIL

filt='

    if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
        if test -n "$TARG_EMAIL"; then
            GIT_AUTHOR_EMAIL="$TARG_EMAIL"
            export GIT_AUTHOR_EMAIL
        else
            unset GIT_AUTHOR_EMAIL
        fi
        if test -n "$TARG_NAME"; then
            GIT_AUTHOR_NAME="$TARG_NAME"
            export GIT_AUTHOR_NAME
        else
            unset GIT_AUTHOR_NAME
        fi
    fi

'

git filter-branch $force --env-filter "$filt" -- $br

首选的答案是,使用gitrebase-i是有效的,但正如在另一个答案中强调的那样,当要编辑的提交周围有合并时,就会变得混乱。使用gitreplace是明智的,但gitfilter分支会重写其他分支和标记的所有历史,这不是我们通常想要的。

我想分享一个替代第一个答案的方法,即使有合并,它仍然很简单。在我的例子中,当我使用gitrebase-I<earlycommit>时,在继续rebase之前,我首先要解决一个冲突。事实上,使用break命令比使用edit命令更容易。并直接重新基于我们的目标提交。

让我们举个例子,假设git日志显示。。。

commit a12afg
...
commit dloe7a
...
commit gh7ag1
...   
commit qp3zaa
...

假设您想更新提交gh7ag1的作者、消息或提交签名。您可以继续使用git rebase-i gh7ag1。在编辑器中,您将看到:

pick dloe7a
pick a12afg

只需添加一个break命令:

break
pick dloe7a
pick a12afg

保存(:wq与VI,Ctrl+O,然后Ctrl+X与nano)。现在,你在承诺后马上回来了。您可以运行gitcommit--modify来更新作者、消息或签名(例如gitcommit--modify-S--author=“Your Name<Your email>”)。使用git-log进行验证--显示签名。如果正确,可以继续使用git-rebase--continue。

在rebase中可以有任意多的break命令。continue将移动到下一个break(如果有),或者应用剩余的提交(如果它们标记为pick)。