我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
首选的答案是,使用gitrebase-i是有效的,但正如在另一个答案中强调的那样,当要编辑的提交周围有合并时,就会变得混乱。使用gitreplace是明智的,但gitfilter分支会重写其他分支和标记的所有历史,这不是我们通常想要的。
我想分享一个替代第一个答案的方法,即使有合并,它仍然很简单。在我的例子中,当我使用gitrebase-I<earlycommit>时,在继续rebase之前,我首先要解决一个冲突。事实上,使用break命令比使用edit命令更容易。并直接重新基于我们的目标提交。
让我们举个例子,假设git日志显示。。。
commit a12afg
...
commit dloe7a
...
commit gh7ag1
...
commit qp3zaa
...
假设您想更新提交gh7ag1的作者、消息或提交签名。您可以继续使用git rebase-i gh7ag1。在编辑器中,您将看到:
pick dloe7a
pick a12afg
只需添加一个break命令:
break
pick dloe7a
pick a12afg
保存(:wq与VI,Ctrl+O,然后Ctrl+X与nano)。现在,你在承诺后马上回来了。您可以运行gitcommit--modify来更新作者、消息或签名(例如gitcommit--modify-S--author=“Your Name<Your email>”)。使用git-log进行验证--显示签名。如果正确,可以继续使用git-rebase--continue。
在rebase中可以有任意多的break命令。continue将移动到下一个break(如果有),或者应用剩余的提交(如果它们标记为pick)。
其他回答
如果您的机器上缺少设置,例如,在格式化之后,或者在没有正确设置(正确)这些命令的情况下没有正确配置Git,则可能会发生这种情况。
git config user.name "Author Name"
git config user.email "<email@address.com>"
为什么不遵循Atlassian的这篇文章,让你的生活更简单?
gitcommit--modify--author=“作者名称<email@address.com>"如果分支受到保护,请取消对其的保护。在本例中,它是master;因此,它将受到源代码存储库的保护git推送原点master—力
这是最后一次提交的最简单场景。要选择任何“随机”提交,您需要:
git rebase-i<早期提交>。更改您感兴趣的提交的编辑选择gitcommit--modify--author=“作者名称<email@address.com>"如果分支受到保护,请取消对其的保护。在本例中,它是master;因此,它将受到源代码存储库的保护git推送原点master—力
在推送之前,您可以随时在两者之间进行git登录,以确定您所在的位置。
在执行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...
你可以删除前几行。
你所链接的问题中的答案是好答案,涵盖了你的情况(另一个问题更为一般,因为它涉及重写多次提交)。
作为尝试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
您可以从github的官方页面使用这些命令
https://help.github.com/en/github/using-git/changing-author-info
这是命令
#!/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
在这里,您可以将旧电子邮件更改为新用户名和电子邮件地址。
对于合并提交消息,我发现我不能通过使用rebase来修改它,至少在gitlab上是这样。它将合并显示为提交,但我无法重新基于该#sha。我发现这篇文章很有用。
git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch
这三行代码完成了更改合并提交消息的工作(如作者)。