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

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


当前回答

如果您使用的是集中存储库,Amber的答案还有一个步骤:

gitpush-f强制更新中央存储库。

请注意,在同一分支上工作的人不多,因为这会破坏一致性。

其他回答

您可以使用下面的命令更改上次提交的作者。

gitcommit--modify--author=“作者名称<email@address.com>"

然而,如果您想更改多个提交的作者名称,这有点棘手。您需要启动一个交互式的rebase,然后将提交标记为edit,然后逐一修改并完成。

使用git rebase-i开始重新定基。它会向你展示这样的东西。

更改pick关键字以编辑要更改作者名称的提交。

然后关闭编辑器。对于初学者,点击Escape,然后键入:wq并点击Enter。

然后你会看到你的终端就像什么都没发生一样。事实上,您正在进行交互式重设基。现在是使用上面的命令修改提交的作者名称的时候了。它将再次打开编辑器。退出并继续使用git rebase重新创建数据库--继续。对要编辑的提交计数重复相同的操作。您可以确保在执行No rebase时完成交互式rebase?消息

在全球范围内更改提交人姓名和电子邮件:

$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"

更改每个存储库的提交人名称和电子邮件:

$ git config user.name "John Doe"
$ git config user.email "john@doe.org"

仅为下一次提交更改作者信息:

$ git commit --author="John Doe <john@doe.org>"

提示:对于其他情况和阅读更多信息,请阅读帖子参考。

作为Eugen Konkov回答的补充,可以保留提交/作者日期。要只修改作者而不修改最后三次提交的日期,请使用

git rebase --onto HEAD~3 --exec 'GIT_COMMITTER_DATE="$(git log -n 1 --format=%aD)" git commit --amend --reset-author --no-edit --date="$(git log -n 1 --format=%aD)"' HEAD~3

然后用力推

git push --force-with-lease

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

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

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