我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
找到一种可以快速改变用户并且不会对其他提交产生副作用的方法。
简单明了的方式:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git log
git rebase -i 1f1357
# change the word 'pick' to 'edit', save and exit
git commit --amend --reset-author --no-edit
git rebase --continue
git push --force-with-lease
详细操作
显示提交日志,并在提交之前找出要更改的提交id:
git log
git从选择的提交id反向开始到最近的提交id:
git config user.name "New User"
git config user.email "newuser@gmail.com"
git rebase -i 1f1357
# change word pick to edit, save and exit
edit 809b8f7 change code order
pick 9baaae5 add prometheus monitor kubernetes
edit 5d726c3 fix liquid escape issue
edit 3a5f98f update tags
pick 816e21c add prometheus monitor kubernetes
rebase将在下一次提交id时停止,输出:
Stopped at 809b8f7... change code order
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
确认并继续您的重新基准,直到它成功地传递给refs/heads/master。
# each continue will show you an amend message
# use git commit --amend --reset-author --no-edit to comfirm
# use git rebase --skip to skip
git commit --amend --reset-author --no-edit
git rebase --continue
git commit --amend --reset-author --no-edit
...
git rebase --continue
Successfully rebased and updated refs/heads/master.
git推送更新
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
这个问题的公认答案是交互式rebase的巧妙使用,但不幸的是,如果我们试图更改作者的提交曾经位于一个随后被合并的分支上,则会出现冲突。更一般而言,它在处理混乱的历史时不起作用。
由于我担心运行依赖于设置和取消设置环境变量来重写git历史的脚本,因此我正在根据这篇文章编写一个新的答案,该答案与此答案类似,但更完整。
与链接的答案不同,以下内容经过测试并有效。为了说明清楚,假设03f482d6是我们试图替换其作者的提交,42627abe是新作者的提交。
签出我们试图修改的提交。git结帐03f482d6让作者改变。gitcommit--modify--author“新作者姓名<新作者电子邮件>”
现在我们有了一个新的提交,哈希值假定为42627abe。
签出原始分支。用本地新提交替换旧提交。git替换03f482d6 42627abe基于替换重写所有未来提交。git筛选器分支--全部拆下替换件以保持清洁。git替换-d 03f482d6推送新的历史记录(仅在以下操作失败时使用--force,并且仅在使用gitlog和/或gitdiff进行健全性检查后使用)。git push—强制执行租约
而不是4-5,你可以重新开始新的提交:
git rebase -i 42627abe
在执行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...
你可以删除前几行。
如果要更改的提交不是最后一次提交,请执行以下步骤。如果您的提交位于不同的分支,那么首先切换到该分支。
git签出分支名称
在要更改的提交之前查找提交并查找其哈希。然后发出rebase命令。
git rebase-i-p提交哈希
然后将打开一个编辑器,并输入“edit”以查看要更改的提交。让其他人保留默认的“选择”选项。一旦更改,输入“esc”键和wq!退出。
然后发出带有修正选项的gitcommit命令。
gitcommit--modify--author=“用户名电子邮件”--无编辑
然后发出以下命令。
git rebase—继续
在本地存储库中更新提交作者后,将更改推送到远程存储库。
如果您只想更改上次提交的作者,可以执行以下操作:
将电子邮件重置为全局配置:git-config--全局用户电子邮件example@email.com现在重置提交的作者,无需编辑:gitcommit--修改--重置作者--无编辑
注意,这也会更改作者时间戳。