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

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


当前回答

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

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

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

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

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

转到您的原始分支:

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

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