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

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


当前回答

使用交互式回扣

git rebase -i -p <some HEAD before all of your bad commits>

然后在rebase文件中将所有错误提交标记为“edit”,当git要求您修改每个提交时

git commit --amend --author "New Author Name <email@address.com>"

编辑或关闭打开的编辑器,然后执行

git rebase --continue

以继续重新启动。

您可以通过附加--no edit来跳过在此处完全打开编辑器,这样命令将是:

git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continue

单次提交

正如一些评论所指出的,如果您只想更改最近的提交,那么rebase命令是不必要的。就这样吧

git commit --amend --author "New Author Name <email@address.com>"

这会将author更改为指定的名称,但committer将设置为gitconfig user.name和gitconfig user.email中配置的用户。如果要将committer设置为指定的值,这将同时设置author和committer:

git -c user.name="New Author Name" -c user.email=email@address.com commit --amend --reset-author

其他回答

作为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

对于这个问题,也有一种懒惰的方法,特别是当您有多个提交需要更改时。在我的案例中,我有一个新的分支,它与错误的作者进行了多次提交,所以是什么帮助了我:

转到您的原始分支:

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

之后,您可以删除具有错误作者的分支。

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

$ 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>"

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

找到一种可以快速改变用户并且不会对其他提交产生副作用的方法。

简单明了的方式:

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

溶液

安装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中找到。