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

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


当前回答

对于合并提交消息,我发现我不能通过使用rebase来修改它,至少在gitlab上是这样。它将合并显示为提交,但我无法重新基于该#sha。我发现这篇文章很有用。

git checkout <sha of merge>
git commit --amend # edit message
git rebase HEAD previous_branch

这三行代码完成了更改合并提交消息的工作(如作者)。

其他回答

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

$ 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 push -f origin last_commit_hash:branch_name 

更改提交的作者名称:

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

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

git push

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

简单明了的方式:

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签出分支名称

在要更改的提交之前查找提交并查找其哈希。然后发出rebase命令。

git rebase-i-p提交哈希

然后将打开一个编辑器,并输入“edit”以查看要更改的提交。让其他人保留默认的“选择”选项。一旦更改,输入“esc”键和wq!退出。

然后发出带有修正选项的gitcommit命令。

gitcommit--modify--author=“用户名电子邮件”--无编辑

然后发出以下命令。

git rebase—继续

在本地存储库中更新提交作者后,将更改推送到远程存储库。

如果您只想更改上次提交的作者,可以执行以下操作:

将电子邮件重置为全局配置:git-config--全局用户电子邮件example@email.com现在重置提交的作者,无需编辑:gitcommit--修改--重置作者--无编辑

注意,这也会更改作者时间戳。