我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
我想改变历史上某个特定承诺的作者。这不是最新的承诺。
相关:如何更改多次提交的作者和提交人姓名/电子邮件?
当前回答
Github文档包含一个脚本,用于替换分支中所有提交的提交者信息(现在无法恢复,这是最后一个快照)。
更改变量值后,从终端运行以下脚本
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
将更正的历史推送到GitHub:
git push --force --tags origin 'refs/heads/*'
或者,如果您想推送选定的分支引用,请使用
git push --force --tags origin 'refs/heads/develop'
其他回答
这个问题的公认答案是交互式rebase的巧妙使用,但不幸的是,如果我们试图更改作者的提交曾经位于一个随后被合并的分支上,则会出现冲突。更一般而言,它在处理混乱的历史时不起作用。
由于我担心运行依赖于设置和取消设置环境变量来重写git历史的脚本,因此我正在根据这篇文章编写一个新的答案,该答案与此答案类似,但更完整。
与链接的答案不同,以下内容经过测试并有效。为了说明清楚,假设03f482d6是我们试图替换其作者的提交,42627abe是新作者的提交。
签出我们试图修改的提交。git结帐03f482d6让作者改变。gitcommit--modify--author“新作者姓名<新作者电子邮件>”
现在我们有了一个新的提交,哈希值假定为42627abe。
签出原始分支。用本地新提交替换旧提交。git替换03f482d6 42627abe基于替换重写所有未来提交。git筛选器分支--全部拆下替换件以保持清洁。git替换-d 03f482d6推送新的历史记录(仅在以下操作失败时使用--force,并且仅在使用gitlog和/或gitdiff进行健全性检查后使用)。git push—强制执行租约
而不是4-5,你可以重新开始新的提交:
git rebase -i 42627abe
如果您需要更改的是上一次提交的作者,而没有其他人正在使用您的存储库,您可以使用以下方法撤消上一次的提交:
git push -f origin last_commit_hash:branch_name
更改提交的作者名称:
git commit --amend --author "type new author here"
退出打开的编辑器并再次推送代码:
git push
可选:如果不想将本地更改发送到远程,请确保将其隐藏。
$ 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公司
找到一种可以快速改变用户并且不会对其他提交产生副作用的方法。
简单明了的方式:
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 -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