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

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


当前回答

溶液

安装git filter repo(git项目建议在filter分支上安装filter repo)$PACKAGE_TOOL安装git筛选器repo在git存储库的根目录中创建一个文件.mailmap,其中包含新名称<new@ema.il> <old@ema.il>运行gitfilter repo--使用mailmap


更多详细信息

gitfilter repo在其文档中将其列为示例不要像上面的示例那样替换名称和电子邮件,请查看gitmailmap文档中的其他示例您可以通过使用参数--mailmap<filename>调用gitfilter repo来指定mailmap文件,而不是默认使用名为.mailmap的文件。关于如何进一步过滤分支/标签/任何内容的更多示例,可以在gitfilter repo项目的README.md中找到。

其他回答

有一个快捷方式适用于投票最多的问题:使用exec而不是edit。

exec允许对指定的提交运行命令。使用它可以避免使用edit、退出终端并为每个git提交运行git命令。如果您必须更改历史记录中的多次提交,这尤其有用。

步骤如下:

执行对早期提交的重新基础(gitrebase-i<earliercommit>)在打开的编辑器中,在要编辑的每个提交行后添加一行,然后添加exec git commit--modify--author=“作者名称<email@address.com>“--不编辑(如果要重置为gitconfig中设置的值,请使用--reset-author)”保存并退出-这将为每次提交运行指定的命令,有效地更改作者

编辑器内容示例(更改前两个提交作者):

pick 1fc6c95 Patch A
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick 6b2481b Patch B
exec git commit --amend --author="Author Name <email@address.com>" --no-edit
pick dd1475d something I want to split
pick c619268 A fix for Patch B
pick fa39187 something to add to patch A
pick 4ca2acc i cant' typ goods
pick 7b36971 something to move before patch B

# Rebase 41a72e6..7b36971 onto 41a72e6
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

我2019年的评论转化为答案:

修复最后六次提交的创作

首先为当前Git repo设置正确的作者git-config--本地user.name“FirstName LastName”git-config--本地用户.emailfirst.last@example.com然后将修复应用于最后六次提交git rebase--on HEAD~6--exec“git commit--modify--reset author--no edit”HEAD~6最后强制推送到远程Git repogit push—强制执行租约

交互式重新基准历史中比您需要修改的提交更早的一点(git rebase-i<earlier commit>)。在要重设基础的提交列表中,将要修改的提交的哈希旁边的文本从pick更改为edit。然后,当git提示您更改提交时,使用以下命令:

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

例如,如果您的提交历史是A-B-C-D-E-F,F是HEAD,并且您想更改C和D的作者,那么您应该。。。

指定gitrebase-i B(以下是执行gitrebase-iB命令后将看到的示例)如果需要编辑A,请使用git-rebase-i--root将C和D的行从拾取更改为编辑退出编辑器(对于vim,这将是按Esc,然后键入:wq)。一旦重新启动,它将首先在C处暂停您可以gitcommit--modify--author=“作者姓名”<email@address.com>"然后git rebase--继续它会在D时再次暂停然后您将gitcommit--modify--author=“作者姓名”<email@address.com>“”再次git rebase—继续重新基础将完成。使用git push-f使用更新的提交更新源代码。

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

作为尝试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

为了进一步解释Eugen Konkov的回答,从根提交开始,使用--root标志。--noedit标志也很有用,因为使用它,每次提交都不会提示您进入编辑器。

git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"