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

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


当前回答

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

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

其他回答

溶液

安装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中找到。

如果您需要更改的是上一次提交的作者,而没有其他人正在使用您的存储库,您可以使用以下方法撤消上一次的提交:

git push -f origin last_commit_hash:branch_name 

更改提交的作者名称:

git commit --amend --author "type new author here"

退出打开的编辑器并再次推送代码:

git push

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

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

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

可选:如果不想将本地更改发送到远程,请确保将其隐藏。

$ git status
$ git stash

更新上次提交的作者。

$ git log   // Old author in local and remote
$ git commit --amend --author="Author Name <email@address.com>"
$ git log   // New Author in local
$ git push origin <branch> --force-with-lease 
$ git log   // New Author in remote

然后,如果您使用git stash,则恢复您的暂存更改

$ git stash pop
$ git status

然后,您应该为当前项目的下一次提交更新配置。

$ git config user.name "Author Name"
$ git config user.email "<email@address.com>"

并使用git-config-edit检查或编辑


澄清:在使用$ggpush-f丢失提交的罕见情况下,您可以使用reflog恢复它们。无论如何,在租约中使用--force比只使用-f更能保护您

GL

来源ZSH公司

Github文档包含一个脚本,用于替换分支中所有提交的提交者信息(现在无法恢复,这是最后一个快照)。

更改变量值后,从终端运行以下脚本

#!/bin/sh
 
git filter-branch --env-filter '
 
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

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

将更正的历史推送到GitHub:

git push --force --tags origin 'refs/heads/*'

或者,如果您想推送选定的分支引用,请使用

git push --force --tags origin 'refs/heads/develop'