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

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


当前回答

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

git push -f origin last_commit_hash:branch_name 

更改提交的作者名称:

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

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

git push

其他回答

在执行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...

你可以删除前几行。

这个问题的公认答案是交互式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

我2019年的评论转化为答案:

修复最后六次提交的创作

首先为当前Git repo设置正确的作者git-config--本地user.name“FirstName LastName”git-config--本地用户.emailfirst.last@example.com然后将修复应用于最后六次提交git rebase--on HEAD~6--exec“git commit--modify--reset author--no edit”HEAD~6最后强制推送到远程Git repogit push—强制执行租约

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

首选的答案是,使用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)。