我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
如果您只想更改上次提交的作者,可以执行以下操作:
将电子邮件重置为全局配置:git-config--全局用户电子邮件example@email.com现在重置提交的作者,无需编辑:gitcommit--修改--重置作者--无编辑
注意,这也会更改作者时间戳。
其他回答
交互式重新基准历史中比您需要修改的提交更早的一点(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
如果您只想更改上次提交的作者,可以执行以下操作:
将电子邮件重置为全局配置: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的答案还有一个步骤:
gitpush-f强制更新中央存储库。
请注意,在同一分支上工作的人不多,因为这会破坏一致性。