我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
有一个快捷方式适用于投票最多的问题:使用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 checkout develop
从中创建新分支:
git checkout -b myFeature develop
将其合并为一次提交,但不包含提交信息:
git merge --no-commit --squash branchWrongAuthor
您可能还想进行更改:
git stage .
更改作者姓名并提交更改:
git commit --amend --author "New Author Name <New Author Email>" -m "new feature added"
就这样,你可以推动改变。
git push
之后,您可以删除具有错误作者的分支。
为了进一步解释Eugen Konkov的回答,从根提交开始,使用--root标志。--noedit标志也很有用,因为使用它,每次提交都不会提示您进入编辑器。
git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"
首选的答案是,使用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 filter repo(git项目建议在filter分支上安装filter repo)$PACKAGE_TOOL安装git筛选器repo在git存储库的根目录中创建一个文件.mailmap,其中包含新名称<new@ema.il> <old@ema.il>运行gitfilter repo--使用mailmap
更多详细信息
gitfilter repo在其文档中将其列为示例不要像上面的示例那样替换名称和电子邮件,请查看gitmailmap文档中的其他示例您可以通过使用参数--mailmap<filename>调用gitfilter repo来指定mailmap文件,而不是默认使用名为.mailmap的文件。关于如何进一步过滤分支/标签/任何内容的更多示例,可以在gitfilter repo项目的README.md中找到。
在全球范围内更改提交人姓名和电子邮件:
$ 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>"
提示:对于其他情况和阅读更多信息,请阅读帖子参考。