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

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


当前回答

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

$ 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公司

其他回答

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

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

你可以删除前几行。

有一个快捷方式适用于投票最多的问题:使用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.
#

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

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

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

git push -f origin last_commit_hash:branch_name 

更改提交的作者名称:

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

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

git push