我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
找到一种可以快速改变用户并且不会对其他提交产生副作用的方法。
简单明了的方式:
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,则可能会发生这种情况。
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 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 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
交互式重新基准历史中比您需要修改的提交更早的一点(git rebase-i<earlier commit>)。在要重设基础的提交列表中,将要修改的提交的哈希旁边的文本从pick更改为edit。然后,当git提示您更改提交时,使用以下命令:
git commit --amend --author="Author Name <email@address.com>" --no-edit
例如,如果您的提交历史是A-B-C-D-E-F,F是HEAD,并且您想更改C和D的作者,那么您应该。。。
指定gitrebase-i B(以下是执行gitrebase-iB命令后将看到的示例)如果需要编辑A,请使用git-rebase-i--root将C和D的行从拾取更改为编辑退出编辑器(对于vim,这将是按Esc,然后键入:wq)。一旦重新启动,它将首先在C处暂停您可以gitcommit--modify--author=“作者姓名”<email@address.com>"然后git rebase--继续它会在D时再次暂停然后您将gitcommit--modify--author=“作者姓名”<email@address.com>“”再次git rebase—继续重新基础将完成。使用git push-f使用更新的提交更新源代码。
可选:如果不想将本地更改发送到远程,请确保将其隐藏。
$ 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公司